package com.park.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.annotation.Resource;

import com.park.model.*;
import com.park.service.*;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.StringUtil;
import com.park.dao.PosChargeDataDAO;

@RequestMapping("barrierCharge")
@Controller
public class BarrierChargeController {
	@Autowired
	HardwareService hardwareService;
	@Autowired
	PosChargeDataService chargeSerivce;
	@Autowired
	PosChargeDataDAO chargeDao;
	@Autowired
	ParkToAliparkService parkToAliparkService;
	@Autowired
	AliParkFeeService aliparkFeeService;
	@Autowired
	MonthUserService monthUserService;
	@Autowired
	AlipayrecordService alipayrecordService;
	@Autowired
	ParkService parkService;
	@Autowired
	FeeCriterionService feeCriterionService;
	@Autowired
	ParkCarAuthorityService parkCarAuthorityService;
	@Autowired
	PosChargeMacService posChargeMacService1;
	@Autowired
	SimilarCarNumberService similarCarNumberService;
	@Autowired
	ParkNoticeAuthorityService parkNoticeAuthorityService;

	@Autowired
	ParkCarAuthority2Service parkcarauthority2Service;
	@Autowired
	CarChannelRecordService carChannelRecordService;
	@Autowired
	ActiveMqService ActiveMqService;
	@Autowired
	MonthuserConsumerRecordService monthuserConsumerService;

	@Resource(name = "jedisClient")
	private JedisClient jedisClient;

	private static final Log logger = LogFactory.getLog(BarrierChargeController.class);
  private String monthEndTimeString=null;
	@RequestMapping(value = "exit", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String exit(@RequestBody Map<String, Object> args) throws ParseException {
		String cardNumber = (String) args.get("cardNumber");
		List<PosChargeData> queryCharges = null;
		String exitDate = (String) args.get("exitDate");
		if (exitDate != null) {
			Date eDate = new SimpleDateFormat(Constants.DATEFORMAT).parse(exitDate);
			try {
				queryCharges = chargeSerivce.getDebt(cardNumber, eDate);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return Utility.createJsonMsg(1002, e);
			}
		} else {
			try {
				queryCharges = chargeSerivce.getDebt(cardNumber);
			} catch (Exception e) {
				return Utility.createJsonMsg(1002, e);
			}
		}
		PosChargeData payRet = queryCharges.get(0);
		payRet.setPaidCompleted(true);
		payRet.setPaidMoney(payRet.getChargeMoney());
		payRet.setUnPaidMoney(0);
		payRet.setOperatorId("道闸");
		int num = chargeSerivce.update(payRet);
		if (num == 1) {
			return Utility.createJsonMsg(1001, "success", payRet);
		} else {
			return Utility.createJsonMsg(1002, "fail");
		}
	}

	@RequestMapping(value = "/file/upload/pictureWithUrl", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public String touchedPictureWithUrl(@RequestBody Map<String, String> args) {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		String base64img = args.get("base64img");
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1002);
			return Utility.gson.toJson(dataMap);
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			dataMap.put("status", 1002);
			return Utility.gson.toJson(dataMap);
		}
		String url = FileUploadService.imgBase64Upload(base64img);
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		List<PosChargeData> posChargeDatas = chargeSerivce.getByCardNumberAndPark(cardNumber, parkId);
		if (posChargeDatas.isEmpty()) {
			dataMap.put("status", 1002);
			dataMap.put("message", "没有记录");
			return Utility.gson.toJson(dataMap);
		}

		if (channelFlag == 1) {
			dataMap.put("status", 1001);
			dataMap.put("message", "入口成功");
			dataMap.put("body", url);
			posChargeDatas.get(0).setUrl(url);

		} else {
			dataMap.put("status", 1001);
			dataMap.put("message", "出口成功");
			dataMap.put("body", url);
			posChargeDatas.get(0).setOutUrl(url);
		}
		chargeSerivce.update(posChargeDatas.get(0));
		return Utility.gson.toJson(dataMap);
	}

	@RequestMapping(value = "/file/upload/touchedPicture", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public void touchedPictureUrl(@RequestBody Map<String, String> args) {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		String base64img = args.get("base64img");
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1003);
			return;
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			return;
		}
		String url = FileUploadService.updateToMinio(base64img);
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		List<PosChargeData> posChargeDatas = chargeSerivce.getByCardNumberAndPark(cardNumber, parkId);
		logger.info("上传图片地址返回:" + url);
		if (posChargeDatas.isEmpty()) {
			return;
		}

		if (channelFlag == 1) {
			posChargeDatas.get(0).setUrl(url);

		} else {
			posChargeDatas.get(0).setOutUrl(url);
		}
		chargeSerivce.update(posChargeDatas.get(0));
	}
/*
 * 	jsonB.put("mac", mac);
					jsonB.put("cardNumber", cardNumber);
					jsonB.put("base64img", imageData);
					jsonB.put("picUrl", "");
 * 
 * */
	@RequestMapping(value = "/file/upload/touchedPictureV2", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public void touchedPictureUrlV2(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		String base64img = args.get("base64img");
		logger.info("touchedPictureV2" + "\t" + cardNumber + "\t" + mac + "\t" + StringUtils.isNotEmpty(base64img));
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			logger.info("touchedPictureV2" + "\t" + cardNumber + "\t" + mac + "\t" + "未找到硬件1");
			dataMap.put("status", 1003);
			return;
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			logger.info("touchedPictureV2" + "\t" + cardNumber + "\t" + mac + "\t" + "未找到硬件2");
			return;
		}
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		String redisId = jedisClient.get("P-" + parkId + "-" + cardNumber);
		PosChargeData posChargeData = null;
		if (StringUtils.isBlank(redisId)) {
			logger.info(cardNumber + "没有redisId值");
			return;
		} else {
			posChargeData = chargeDao.getById(Integer.parseInt(redisId));
			logger.info(cardNumber + "\t" + redisId);
		}
		if (posChargeData == null || posChargeData.getParkDesc().contains("包月车")) {
			logger.info(cardNumber + "包月车");
			return;
		}
		if (channelFlag == 1) {
			if (StringUtils.isNotBlank(posChargeData.getUrl())) {
				logger.info(cardNumber + "有入场图片");
				return;
			}
			if (new Date().getTime() - posChargeData.getEntranceDate().getTime() > 600000) {
				logger.info(cardNumber + "入场时间大于10分钟");
				return;
			}
		}
		if (channelFlag == 0) {
			if (StringUtils.isNotBlank(posChargeData.getOutUrl())) {
				logger.info(cardNumber + "有出场图片");
				return;
			}
			if (new Date().getTime() - posChargeData.getExitDate().getTime() > 600000) {
				logger.info(cardNumber + "出场时间大于10分钟");
				return;
			}
		}
		String url = null;
		String outUrl = null;
		String inUrl = null;
		if (StringUtils.isNotEmpty(base64img)) {
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("data", base64img);
			Map<String, Object> result = new HashMap<String, Object>();
			result = HttpUtil.post("http://192.168.60.3:8105/files/img/base64", data);
			logger.info(cardNumber + "\t" + result);
			JSONObject jsonResult = JSONObject.parseObject(result.get("body").toString());
			url = jsonResult.getString("body");
			logger.info(cardNumber + "\t" + url);
		} else {
			url = args.get("picUrl");
		}

		if (channelFlag == 1) {
			inUrl = url;
			if (jedisClient.get("record-in" + parkId + cardNumber) != null) {
				CarChannelRecord carChannelRecord = carChannelRecordService
						.selectById(Long.valueOf(jedisClient.get("record-in" + parkId + cardNumber)));
				carChannelRecord.setImgUrl(url);
				carChannelRecordService.updateBySelective(carChannelRecord);
			}
		} else {
			outUrl = url;
			if (jedisClient.get("record-out" + parkId + cardNumber) != null) {
				CarChannelRecord carChannelRecord = carChannelRecordService
						.selectById(Long.valueOf(jedisClient.get("record-out" + parkId + cardNumber)));
				carChannelRecord.setImgUrl(url);
				carChannelRecordService.updateBySelective(carChannelRecord);
			}
		}

		int result = chargeSerivce.updatePicture(posChargeData.getId(), inUrl, outUrl);
	}

	@RequestMapping(value = "/file/upload/touchedPictureV3", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public void touchedPictureUrlV3(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		String base64img = args.get("base64img");
		logger.info("touchedPictureV3" + "\t" + cardNumber + "\t" + mac + "\t" + StringUtils.isNotEmpty(base64img));
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		logger.info(cardNumber + "\t"+infos);
		if (infos.isEmpty()) {
			logger.info("touchedPictureV3" + "\t" + cardNumber + "\t" + mac + "\t" + "未找到硬件1");
			dataMap.put("status", 1003);
			return;
		}
		Map<String, Object> info = infos.get(0);
		logger.info(cardNumber + "\t"+info);
		if (info == null) {
			logger.info("touchedPictureV3" + "\t" + cardNumber + "\t" + mac + "\t" + "未找到硬件2");
			return;
		}
		int channelFlag = (int) info.get("channelFlag");
		logger.info(cardNumber + "\t"+channelFlag);
		Integer parkId = (Integer) info.get("parkID");
		logger.info(cardNumber + "\t"+parkId);
		String redisId = jedisClient.get("P-" + parkId + "-" + cardNumber);
		logger.info(cardNumber + "获取redis:"+redisId);
		PosChargeData posChargeData = null;
		if (StringUtils.isBlank(redisId)) {
//	posChargeData = chargeDao.getByCardNumberAndParkV2(cardNumber, parkId);
			logger.info(cardNumber + "没有redisId值");
			return;
		} else {
			posChargeData = chargeDao.getById(Integer.parseInt(redisId));
			logger.info(cardNumber + "\t" + redisId);
		}
		if (posChargeData == null || posChargeData.getParkDesc().contains("包月车")) {
			logger.info(cardNumber + "包月车");
			return;
		}
		if (channelFlag == 1) {
			if (StringUtils.isNotBlank(posChargeData.getUrl())) {
				logger.info(cardNumber + "有入场图片");
				return;
			}
			if (new Date().getTime() - posChargeData.getEntranceDate().getTime() > 600000) {
				logger.info(cardNumber + "入场时间大于10分钟");
				return;
			}
		}
		if (channelFlag == 0) {
			if (StringUtils.isNotBlank(posChargeData.getOutUrl())) {
				logger.info(cardNumber + "有出场图片");
				return;
			}
			if (new Date().getTime() - posChargeData.getExitDate().getTime() > 600000) {
				logger.info(cardNumber + "出场时间大于10分钟");
				return;
			}
		}
		String url = null;
		String outUrl = null;
		String inUrl = null;
		if (StringUtils.isNotEmpty(base64img)) {
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("data", base64img);
			Map<String, Object> result = new HashMap<String, Object>();
			result = HttpUtil.post("http://192.168.60.3:8105/files/img/base64", data);
			logger.info(cardNumber + "\t" + result);
			JSONObject jsonResult = JSONObject.parseObject(result.get("body").toString());
			url = jsonResult.getString("body");
			logger.info(cardNumber + "\t" + url);
		} else {
			url = args.get("picUrl");
		}
		if (channelFlag == 1) {
			inUrl = url;
			if (jedisClient.get("record-in" + parkId + cardNumber) != null) {
				CarChannelRecord carChannelRecord = carChannelRecordService
						.selectById(Long.valueOf(jedisClient.get("record-in" + parkId + cardNumber)));
				carChannelRecord.setImgUrl(url);
				carChannelRecordService.updateBySelective(carChannelRecord);
			}
		} else {
			outUrl = url;
			if (jedisClient.get("record-out" + parkId + cardNumber) != null) {
				CarChannelRecord carChannelRecord = carChannelRecordService
						.selectById(Long.valueOf(jedisClient.get("record-out" + parkId + cardNumber)));
				carChannelRecord.setImgUrl(url);
				carChannelRecordService.updateBySelective(carChannelRecord);
			}
		}

		int result = chargeSerivce.updatePicture(posChargeData.getId(), inUrl, outUrl);
	}

	@RequestMapping(value = "touchedMonthUser", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public void touchedMonthUser(@RequestBody Map<String, String> args) throws Exception {
		logger.info("[touchedMonthUser]参数:" + args);
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		PosChargeData charge = new PosChargeData();

		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			logger.info("没有找到该硬件设备");
			dataMap.put("status", 1003);
			return;
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			return;
		}
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		Park park = parkService.getParkById(parkId);
		List<Monthuser> monthusers = monthUserService.getLast3Number(cardNumber, parkId);
		if (monthusers.isEmpty()) {
			logger.info(cardNumber + "后三位没有包月");
			return;
		}
		cardNumber = monthusers.get(0).getPlatenumber();
		if (channelFlag == 1) {
			charge.setParkDesc(park.getName() + "-3包月车");
			charge.setCardNumber(cardNumber);
			charge.setParkId(parkId);
			charge.setRejectReason(monthusers.get(0).getOwner());
			charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			int num = chargeSerivce.insert(charge);
			logger.info("channelFlag == 1");
			if (num == 1) {
				// Poschargemac poschargemac = new Poschargemac();
				// poschargemac.setMacidenter((int) info.get("macId"));
				// poschargemac.setPoschargeid(charge.getId());
				// posChargeMacService.insertSelective(poschargemac);
			}
		} else {
			logger.info("channelFlag != 1");
			List<PosChargeData> queryCharges = chargeSerivce.getDebt(cardNumber);
		}
	}

	@RequestMapping(value = "touchedMonthUser4", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public void touchedMonthUser4(@RequestBody Map<String, String> args) throws Exception {
		logger.info("[touchedMonthUser]参数:" + args);
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		PosChargeData charge = new PosChargeData();

		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			logger.info("没有找到该硬件设备");
			dataMap.put("status", 1003);
			return;
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			return;
		}
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		Park park = parkService.getParkById(parkId);
		List<Monthuser> monthusers = monthUserService.getLast4Number(cardNumber, parkId);
		if (monthusers.isEmpty()) {
			logger.info(cardNumber + "后4位没有包月");
			return;
		}
		cardNumber = monthusers.get(0).getPlatenumber();
		if (channelFlag == 1) {
			charge.setParkDesc(park.getName() + "-4包月车");
			charge.setCardNumber(cardNumber);
			charge.setParkId(parkId);
			charge.setRejectReason(monthusers.get(0).getOwner());
			charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			int num = chargeSerivce.insert(charge);
			logger.info("channelFlag == 1");
			if (num == 1) {
				// Poschargemac poschargemac = new Poschargemac();
				// poschargemac.setMacidenter((int) info.get("macId"));
				// poschargemac.setPoschargeid(charge.getId());
				// posChargeMacService.insertSelective(poschargemac);
			}
		} else {
			logger.info("channelFlag != 1");
			List<PosChargeData> queryCharges = chargeSerivce.getDebt(cardNumber);
		}
	}

	public void CarInSendMq(int parkId, PosChargeData posChargeData, Park park, List<Parktoalipark> parktoaliparks) {

		// 贵州队列发送
		if (parkId == 334) {
			Map<String, String> guiyang = new HashMap<>();
			guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
			guiyang.put("type", "in");
			// guiyang.put("plateNum", cardNumber);
			guiyang.put("enterTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
		}

	}

	public void CarOutSendMq() {

	}

	public double getMoney(PosChargeData charge) {
		if (charge.isPaidCompleted()) {
			return charge.getPaidMoney();
		}
		double money = charge.getChargeMoney();
		if(charge.getDiscountType() == 1) {//全免优惠券
			money = 0;
		}else if(charge.getDiscountType() == 2) {//金额优惠券
			money = money - charge.getDiscount() > 0  ? money - charge.getDiscount() : 0;
		}else if(charge.getDiscountType() == 3) {//固定金额优惠券
			money = charge.getDiscount();
		}
		return money;
	}
	@RequestMapping(value = "touched", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String touched(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		cardNumber = cardNumber.trim();
		logger.info("[touched]进入车辆入场/出场接口,车牌号： " + cardNumber);

		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		PosChargeData charge = new PosChargeData();
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1003);
			return Utility.gson.toJson(dataMap);
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			dataMap.put("aT", "1");
			dataMap.put("cT", "in");
			dataMap.put("uT", "0");
			return Utility.createJsonMsg(1001, dataMap);
		}
		String macName = (String) info.get("description");
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		// 相似车牌管理
		List<Similarcarnumber> similarcarnumbers = similarCarNumberService.selectBySimilarCarNumberAndPark(cardNumber,
				parkId);
		if (!similarcarnumbers.isEmpty()) {
			cardNumber = similarcarnumbers.get(0).getRealnumber();
			logger.info("更正车牌:" + cardNumber);
		}

		List<Monthuser> monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);// 包月用户信息
		Park park = parkService.getParkById(parkId);
		List<Parkcarauthority> parkcarauthorities = parkCarAuthorityService.getByParkId(parkId);
		if (parkcarauthorities.isEmpty()) {
			return null;
		}

		dataMap.put("aT", "1");

		boolean isMonthUser = false;
		boolean isRealMonthUser = false;
		int monthUserType = 9;
		Monthuser monthuserNow = null;
		
		String monthEndTimeString=null;
		
		if (!monthusers.isEmpty()) {
			isMonthUser = true;
			for (Monthuser tmpMonthuser : monthusers) {
				if (tmpMonthuser.getType() == 17) {
					dataMap.put("aT", "0");
					dataMap.put("uT", "17");
					dataMap.put("cT", "in");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				logger.info(cardNumber + "月卡id:" + tmpMonthuser.getId() + "类型:" + tmpMonthuser.getType() + "到期时间:"
						+ Utility.formatDatetime(tmpMonthuser.getEndtime()));
				
				monthEndTimeString=Utility.formatDatetime(tmpMonthuser.getEndtime());
				
				if (park.getDescription().contains("月卡7天延期")) {
					tmpMonthuser.setEndtime1(new Date(tmpMonthuser.getEndtime().getTime() + 7 * 24 * 60 * 60 * 1000));
				}
				Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
				monthuserNow = tmpMonthuser;
				boolean isVip = tmpMonthuser.getType() >= 9 && tmpMonthuser.getType() <= 13;
				if (tmpMonthuser.getType() == 0 || isVip) {// 月卡
					monthUserType = 0;
					if (diff > 0) {
						int leftDays = (int) (diff / (1000 * 60 * 60 * 24));// 月卡到期时间
						dataMap.put("ds", String.valueOf(leftDays));
						dataMap.put("my", "0.0");
						isRealMonthUser = true;
						dataMap.put("eD", new SimpleDateFormat(Constants.DATEFORMAT).format(tmpMonthuser.getEndtime()));
						break;
					} else {
						dataMap.put("ds", "-1");
						dataMap.put("uT", "1");
						monthUserType = 8; // 月卡过期
					}

				} else {
					if (diff >= 0) {
						monthUserType = tmpMonthuser.getType();// 月卡类型
						break;
					} else {
						isMonthUser = false;
					}
				}
			}
		}

		if (!isMonthUser) {// ??
			dataMap.put("uT", "0");
		} else {
			dataMap.put("uT", "1");
			// 判断是否是预约车
			if (monthUserType == 1) {
				dataMap.put("uT", "2");
			}
		}

		if (!monthusers.isEmpty() && monthusers.get(0).getType() == 2) {
			Long diff = (monthusers.get(0).getEndtime().getTime() - (new Date()).getTime());
			if (diff > 0) {
				dataMap.put("uT", "3");
				dataMap.put("yue", String.valueOf(monthusers.get(0).getPayment()));
			} else {
				dataMap.put("uT", "1");
				dataMap.put("ds", "-1");
			}
		}

		List<Parktoalipark> parktoaliparks = parkToAliparkService.getByParkId(parkId);// ??
		Boolean isMultiCarsOneCarport = false;
		if (park.getDescription() != null && park.getDescription().contains("一位多车")) {
			logger.info(cardNumber + "进入一位多车停车场");
			isMultiCarsOneCarport = true;
		}
		Boolean isMultiFeeCtriterion = false;
		if (park.getDescription() != null && park.getDescription().contains("多计费标准")) {
			isMultiFeeCtriterion = true;
		}
		// 判断是否有多个车
		List<Monthuser> realMonthUsers = new ArrayList<>();
		if (monthuserNow != null && monthuserNow.getCardnumber() != null
				&& !monthuserNow.getCardnumber().trim().equals("")) {
			List<Monthuser> monthuserss = monthUserService.getByParkAndPort(monthuserNow.getParkid(),
					monthuserNow.getCardnumber());
			for (Monthuser monthuser : monthuserss) {
				logger.info(cardNumber + "多个车" + monthuser.getId());
				if (monthuser.getType().intValue() == 0) {
					realMonthUsers.add(monthuser);
				}
			}
		}

		Parknoticeauthority parknoticeauthority = parkNoticeAuthorityService.getByParkId(parkId).get(0);
		Parkcarauthority2 parkcarauthority2 = parkcarauthority2Service.selectByPark(parkId);

		charge.setParkDesc(park.getName() + "-临停车");

		// 入口硬件
		if (channelFlag == 1) {
			dataMap.put("lT", String.valueOf(park.getPortLeftCount()));
			if (largeCar == true) {
				charge.setIsLargeCar(true);
			}
			logger.info(cardNumber + "--->开始入场，mac" + mac);
			// 取得入口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 1) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			dataMap.put("cT", "in");
			charge.setCardNumber(cardNumber);
			charge.setParkId(parkId);
			charge.setEnterMac(mac);
			// 判断包月车已入场

			Boolean isMonthUserCarIn = false;
			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				for (Monthuser tmMonthuser : realMonthUsers) {
					if (tmMonthuser.getPlatecolor() != null && tmMonthuser.getPlatecolor().equals("多车包月入场")) {
						logger.info(cardNumber + "同车位已经入场Id" + tmMonthuser.getId());
						if (!tmMonthuser.getPlatenumber().equals(cardNumber)) {
							isMonthUserCarIn = true;
						}

					}
				}
				monthuserNow.setPlatecolor("多车包月入场");
				if (isMonthUserCarIn) {
					charge.setParkDesc(park.getName() + "-包月转临停");
					monthuserNow.setPlatecolor("包月转临停");
					dataMap.put("uT", "0");
					monthUserType = 9;
					try {
						dataMap.remove("ds");
						dataMap.remove("eD");
					} catch (Exception e) {
						// TODO: handle exception
					}

				}
				monthUserService.updateByPrimaryKeySelective(monthuserNow);
			}

			switch (monthUserType) {
			case 0:
				charge.setParkDesc(park.getName() + "-包月车");
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");
					dataMap.put("uT", "1");
				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");
					dataMap.put("uT", "2");
				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (!charge.getParkDesc().contains("包月转临停")) {
					charge.setParkDesc(park.getName() + "-临停车");
				}

				if (parkcarauthority.getTemporary() != true) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority2 != null) {
					logger.info("临停数量" + parkcarauthority2.getCount());
					if (parkcarauthority2.getCount() >= parkcarauthority2.getMaxcount()) {
						dataMap.put("aT", "0");
						logger.info(park.getId() + "-" + cardNumber + "-临停数量达到限制,无法入场");
					} else {
						parkcarauthority2.setCount(parkcarauthority2.getCount() + 1);
						parkcarauthority2Service.updateByPrimaryKey(parkcarauthority2);
					}
				}
				break;
			default:
				break;
			}

			if (!monthusers.isEmpty()) {
				charge.setRejectReason(monthusers.get(0).getOwner());
			}
			charge.setPosId(macName);
			charge.setEnterMacName(macName);
			charge.setEnterMac(mac);
			charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			int num = 0;
			// redis查询
			try {
				String redisStr = jedisClient.get("carIn" + parkId + charge.getCardNumber());
				if (redisStr != null) {
					num = 1;
					logger.info("redis检测重复入场" + cardNumber + redisStr);
				} else {

					// 12小时重复检测
					if (park.getDescription().contains("12小时内免费")
							&& jedisClient.get("P-" + park.getId() + "-12-" + cardNumber) != null) {
						logger.info(cardNumber + "12小时内免费");
						charge.setOther2(jedisClient.get("P-" + park.getId() + "-12-" + cardNumber));
					}

					num = chargeSerivce.insert(charge);
					// 清场
					String redisId = jedisClient.get("P-" + park.getId() + "-" + cardNumber);
					if (redisId != null) {
						PosChargeData posChargeDatatmp = chargeDao.getById(Integer.parseInt(redisId));
						if (posChargeDatatmp != null && !posChargeDatatmp.isPaidCompleted()) {
							posChargeDatatmp.setChargeMoney(0);
							posChargeDatatmp.setExitDate1(posChargeDatatmp.getEntranceDate());
							posChargeDatatmp.setPaidCompleted(true);
							posChargeDatatmp.setOperatorId("redis");
							posChargeDatatmp.setRejectReason("平台清场r");
							logger.info(cardNumber + "清场" + posChargeDatatmp.getId());
							// chargeSerivce.update(posChargeDatatmp);
							chargeSerivce.deleteById(posChargeDatatmp.getId());
						}

						jedisClient.del("P-" + parkId + "-" + charge.getCardNumber());
						jedisClient.del("query" + charge.getCardNumber());
						logger.info("重新入场 " + cardNumber + " id" + charge.getId());

					}

					jedisClient.set("carIn" + parkId + charge.getCardNumber(), String.valueOf(charge.getId()), 60);
					logger.info(cardNumber + "设置redis值" + charge.getId());
					jedisClient.set("carInTmp" + parkId + charge.getCardNumber(), "tmp", 20);
					jedisClient.set("query" + charge.getCardNumber(), String.valueOf(charge.getId()), 259200);
					jedisClient.set("P-" + parkId + "-" + charge.getCardNumber(), String.valueOf(charge.getId()),
							Constants.carMaxPeriod);

				}
			} catch (Exception e) {
				// TODO: handle exception
				logger.info(cardNumber + "redis异常" + e);
				dataMap.put("aT", "1");
				dataMap.put("cT", "in");
				dataMap.put("uT", "0");
				carChannelRecordService.carInRecord(park, charge, args, info);
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				// num = chargeSerivce.insert(charge);
			}

			if (num == 1) {

				// 发送topic队列
				if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
						|| park.getDescription().contains("本地计费")) {
					charge.setEntrance(true);
					ActiveMqService.SendTopicWithMac(charge, String.valueOf(parkId), mac, park.getPortLeftCount(),
							monthUserType);
				}

				try {

					if (!parktoaliparks.isEmpty()) {
						Parktoalipark parktoalipark = parktoaliparks.get(0);
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", parktoalipark.getAliparkingid());
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");

					}
					if (parknoticeauthority != null && parknoticeauthority.getAlipay() == true) {
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", "PI1501317472942184881");
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");
					}
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e);
				}
				logger.info(cardNumber + "入场成功!--" + dataMap.toString());
				ret.put("status", 1001);
			} else {
				ret.put("status", 1002);
			}
			carChannelRecordService.carInRecord(park, charge, args, info);
			return Utility.createJsonMsgWithoutMsg(1001, dataMap);
		} else {// 出场硬件
			logger.info(cardNumber + "--->开始出场，mac " + mac);
			dataMap.put("cT", "out");
			dataMap.put("pT", "9");
			List<PosChargeData> queryCharges = null;
			String exitDate = (String) args.get("exitDate");

			if (jedisClient.get("carInTmp" + parkId + cardNumber) != null) {
				dataMap.put("aT", "2");
				dataMap.put("my", "0.0");
				logger.info(cardNumber + "一分钟内 不许出场,返回" + dataMap.toString());
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			}
			logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
			int portLeftCount = park.getPortLeftCount() + 1;
			parkService.updateLeftPortCount(parkId, portLeftCount);
			logger.info(cardNumber + "离开" + park.getName() + "后车位数" + portLeftCount);
			if (monthuserNow != null) {
				realMonthUsers.add(monthuserNow);
			}

			// 取得出口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 0) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			if (exitDate != null) {
				try {
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					logger.info(cardNumber + "计费异常" + e.getMessage());
					dataMap.put("aT", "1");
					dataMap.put("cT", "out");
					dataMap.put("eD", "13");
					dataMap.put("my", "0.0");
					dataMap.put("pT", "9");
					dataMap.put("uT", "0");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
			} else {
				try {
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);

				} catch (Exception e) {
					logger.info(cardNumber + "计费异常" + e.getMessage());
					dataMap.put("aT", "1");
					dataMap.put("cT", "out");
					dataMap.put("eD", "13");
					dataMap.put("my", "0.0");
					dataMap.put("pT", "9");
					dataMap.put("uT", "0");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
			}
			int mianfeifenzhongshu=48*60;
			// 子母停车场
			if (park.getDescription() != null && park.getDescription().contains("子停车场最高优惠分钟数-")) {
				park.setDescription(park.getDescription().replace("；", ";"));
				String[] descriptions2 = park.getDescription().split(";");
								
				String fenzhongString = "";
				for (String stringd : descriptions2) {
					stringd=stringd.trim();
					if (stringd.contains("子停车场最高优惠分钟数")) {
						fenzhongString = stringd.split("-")[1];
					}
				}
				logger.info(cardNumber+"最高优惠分钟数"+fenzhongString);
			mianfeifenzhongshu=Integer.valueOf(fenzhongString);
			}
			
			logger.info(queryCharges);
			logger.info(cardNumber + "计费结束!");
			double shouldChargeMoney = 0;
			if ((!queryCharges.isEmpty()) && (queryCharges.get(0).getParkDesc().contains("储值卡")) && isMonthUser) {
				if (monthusers.get(0).getPayment() > queryCharges.get(0).getPaidMoney()) {
					logger.info("公版储值卡逻辑");
					if (queryCharges.get(0).getPayType() == 2 || queryCharges.get(0).getPayType() == 9) {
						if (queryCharges.get(0).getPaidMoney() > 0) {
							storageCardLogic(monthusers, queryCharges.get(0));
						}
						dataMap.put("aT", "1");
						dataMap.put("cT", "out");
						dataMap.put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
								queryCharges.get(0).getEntranceDate().getTime()));
						dataMap.put("my", String.valueOf(queryCharges.get(0).getPaidMoney()));
						dataMap.put("pT", "20");
						dataMap.put("uT", "3");
						queryCharges.get(0).setPaidCompleted(true);
						queryCharges.get(0).setPayTime(new SimpleDateFormat(Constants.DATEFORMAT).format(new Date()));
						queryCharges.get(0).setPayType(20);
						chargeSerivce.update(queryCharges.get(0));
						monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
						dataMap.put("yue", String.valueOf(monthusers.get(0).getPayment()));
						logger.info(cardNumber + "出场成功" + "\t" + dataMap);
						jedisClient.set("out" + mac, String.valueOf(queryCharges.get(0).getId()));
						return Utility.createJsonMsgWithoutMsg(1001, dataMap);
					}

					// 微信支付
					else {
						dataMap.put("aT", "1");
						dataMap.put("cT", "out");
						dataMap.put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
								queryCharges.get(0).getEntranceDate().getTime()));
						dataMap.put("my", String.valueOf(queryCharges.get(0).getPaidMoney()));
						dataMap.put("pT", String.valueOf(queryCharges.get(0).getPayType()));
						dataMap.put("uT", "3");
						queryCharges.get(0).setPaidCompleted(true);
						queryCharges.get(0).setPayTime(new SimpleDateFormat(Constants.DATEFORMAT).format(new Date()));
						chargeSerivce.update(queryCharges.get(0));
						monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
						dataMap.put("yue", String.valueOf(monthusers.get(0).getPayment()));
						logger.info(cardNumber + "出场成功" + "\t" + dataMap);
						jedisClient.set("out" + mac, String.valueOf(queryCharges.get(0).getId()));
						return Utility.createJsonMsgWithoutMsg(1001, dataMap);
					}
				} else {
					// 储值卡余额不足
					if (queryCharges.get(0).getPayType() == 2 || queryCharges.get(0).getPayType() == 9) {

						dataMap.put("aT", "0");
						dataMap.put("cT", "out");
						dataMap.put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
								queryCharges.get(0).getEntranceDate().getTime()));
						dataMap.put("my", String.valueOf(queryCharges.get(0).getPaidMoney()));
						dataMap.put("pT", "30");
						dataMap.put("uT", "3");
//						queryCharges.get(0).setPaidCompleted(true);
						queryCharges.get(0).setPayType(30);
						queryCharges.get(0).setPayTime(new SimpleDateFormat(Constants.DATEFORMAT).format(new Date()));
						chargeSerivce.update(queryCharges.get(0));
						monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
						dataMap.put("yue", String.valueOf(monthusers.get(0).getPayment()));
						queryCharges.get(0).setEntrance(false);
						ActiveMqService.SendTopicWithMac(queryCharges.get(0), String.valueOf(parkId), mac,
								park.getPortLeftCount(), monthUserType);
						logger.info(cardNumber + "出场成功" + "\t" + dataMap);
						jedisClient.set("out" + mac, String.valueOf(queryCharges.get(0).getId()));
						return Utility.createJsonMsgWithoutMsg(1001, dataMap);
					} else {
						dataMap.put("aT", "1");
						dataMap.put("cT", "out");
						dataMap.put("eD", getParkingTimeLen(queryCharges.get(0).getExitDate().getTime(),
								queryCharges.get(0).getEntranceDate().getTime()));
						dataMap.put("my", String.valueOf(queryCharges.get(0).getPaidMoney()));
						dataMap.put("pT", String.valueOf(queryCharges.get(0).getPayType()));
						dataMap.put("uT", "3");
						queryCharges.get(0).setPaidCompleted(true);
						queryCharges.get(0).setPayTime(new SimpleDateFormat(Constants.DATEFORMAT).format(new Date()));
						chargeSerivce.update(queryCharges.get(0));
						monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
						dataMap.put("yue", String.valueOf(monthusers.get(0).getPayment()));
						logger.info(cardNumber + "出场成功" + "\t" + dataMap);
						jedisClient.set("out" + mac, String.valueOf(queryCharges.get(0).getId()));
						return Utility.createJsonMsgWithoutMsg(1001, dataMap);
					}

				}
			}
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {
				logger.info(cardNumber + "没有未缴费!");
				FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());
				if (isRealMonthUser && realMonthUsers.size() == 1) {
					logger.info(cardNumber + "月卡用户直接返回!" + dataMap.toString());
					carChannelRecordService.carOutRecord(park, null, args, info, (byte) 4);
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 以下就是查询停车费状态的部分迁移
				logger.info(cardNumber + "开始查询最后一次记录!");
				List<PosChargeData> posChargeDataList = chargeSerivce.getLastRecordWithPark(cardNumber, 1, parkId);
//				List<PosChargeData> posChargeDataList = null;
				if (posChargeDataList.isEmpty() || (posChargeDataList.get(0).getExitDate() != null)) {
					dataMap.put("my", "0.0");
					dataMap.put("eD", "13");
					if (park.getDescription().contains("人工处理")) {
						dataMap.put("aT", "0");
						dataMap.put("uT", "101");
						PosChargeData posCharge = new PosChargeData();
						posCharge.setCardNumber("苏A99999");
						posCharge.setEntrance(false);
						posCharge.setPayType(11);
						posCharge.setEntranceDate(new Date());
						ActiveMqService.SendTopicWithMac(posCharge, String.valueOf(parkId), mac,
								park.getPortLeftCount(), monthUserType);
				logger.info(cardNumber + "在此停车场没有任何记录!" + dataMap.toString());
				carChannelRecordService.carOutRecord(park, null, args, info, (byte) 3);
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
					}
					
				}
				PosChargeData posChargeData = posChargeDataList.get(0);
				logger.info(cardNumber + "最后一次记录id" + posChargeData.getId() + "paytype" + posChargeData.getPayType());
				// 支付类型
				dataMap.put("pT", String.valueOf(posChargeData.getPayType()));
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
				}
				Date payDate = posChargeData.getExitDate();
				if (posChargeData.getPayType() != 2 || posChargeData.getPayType() != 9) {
					logger.info(cardNumber + "已经通过移动进行支付");
					List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
					if (alipayrecords.isEmpty()) {
						payDate = new Date();
					} else {
						payDate = alipayrecords.get(0).getDate();
						logger.info(
								cardNumber + "已查询到支付时间!" + new SimpleDateFormat(Constants.DATEFORMAT).format(payDate));
					}

				}
				if (payDate == null) {
					payDate = new Date();
				}
				long diff = new Date().getTime() - payDate.getTime();
				// 防止逃费
				String[] descriptions = park.getDescription().replace("；", ",").split(";");
				String dealyStr = "";
				for (String tmpStr : descriptions) {
					if (tmpStr.contains("防逃费")) {
						dealyStr = tmpStr.split("-")[1];
					}
				}
				Long maxDelay = (long) 12;
				if (StringUtils.isNotBlank(dealyStr)) {
					maxDelay = (long) Integer.parseInt(dealyStr);
				}
				Long second = (long) 1000 * (long) 60 * (long) 60 * maxDelay;
				if ((posChargeData.getPayType() == 9) && (diff < second)) {
					dataMap.put("my", String.valueOf(getMoney(posChargeData)));
					if (posChargeData.getChargeMoney() > 0.1) {
						dataMap.put("aT", "0");
					} else {
						dataMap.put("aT", "1");
					}

					dataMap.put("eD", String.valueOf((int) Math
							.ceil((new Date().getTime() - posChargeData.getEntranceDate().getTime()) / (1000 * 60))));
					logger.info(cardNumber + "防逃费:" + dataMap.toString());

					jedisClient.set("out" + mac, String.valueOf(posChargeData.getId()));
					// 发送到topic
					if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
							|| park.getDescription().contains("本地计费")) {
						posChargeData.setEntrance(false);
						ActiveMqService.SendTopicWithMac(posChargeData, String.valueOf(parkId), mac,
								park.getPortLeftCount(), monthUserType);
					}
					carChannelRecordService.carOutRecord(park, posChargeData, args, info, (byte) 3);
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}

				if (diff < 1000 * 60 * 30) {
					logger.info(cardNumber + "在支付完15分钟内离场");
					dataMap.put("my", String.valueOf(getMoney(posChargeData)));
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					long diff1 = (posChargeData.getExitDate().getTime() - posChargeData.getEntranceDate().getTime());
					dataMap.put("eD", String.valueOf((int) Math.ceil((diff1 / (1000 * 60)))));

					posChargeData.setPaidCompleted(true);
					posChargeData.setPayTime(new SimpleDateFormat(Constants.DATEFORMAT).format(new Date()));
					posChargeData.setUnPaidMoney(0);
					posChargeData.setPosId(posChargeData.getPosId() + "-" + macName);
					// if (posChargeData.getExitDate() == null) {
					// //posChargeData.setExitDate1(new Date());
					// posChargeData.setExitDate1(payDate);
					// }
					chargeSerivce.update(posChargeData);
					logger.info(cardNumber + "小于15分钟返回" + dataMap.toString());
					carChannelRecordService.carOutRecord(park, posChargeData, args, info, (byte) 1);
					// 子母停车场
					if (park.getDescription() != null && park.getDescription().contains("母停车场")) {
						String[] descriptions2 = park.getDescription().split("；");
						if (descriptions2.length == 1) {
							descriptions2 = park.getDescription().split(";");
						}
						String parkidStr = "";
						for (String stringd : descriptions2) {
							if (stringd.contains("母停车场")) {
								parkidStr = stringd.split("-")[1];
							}
						}
						try {
							if (StringUtils.isNotBlank(parkidStr)
									&& StringUtils.isNotBlank(jedisClient.get("P-" + parkidStr + "-" + cardNumber))) {
								int mPosChargeId = Integer
										.parseInt(jedisClient.get("P-" + parkidStr + "-" + cardNumber));
								logger.info(cardNumber + " 查找到母停车场id " + parkidStr + "poschargeId " + mPosChargeId);
								PosChargeData mPosChargeData = chargeSerivce.getById(mPosChargeId);
								if (mPosChargeData.getId() < posChargeData.getId()) {
									Date newEntranceTime = new Date(mPosChargeData.getEntranceDate().getTime()
											+ posChargeData.getExitDate().getTime()
											- posChargeData.getEntranceDate().getTime());
									if (newEntranceTime.getTime() < System.currentTimeMillis()) {

										// mPosChargeData.setEntranceDate1(newEntranceTime);
										SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
										Calendar nowTime = Calendar.getInstance();
										if (StringUtils.isBlank(mPosChargeData.getValidTimeStart())) {
											nowTime.setTime(mPosChargeData.getEntranceDate());
										} else {
											nowTime.setTime(sdf.parse(mPosChargeData.getValidTimeStart()));
											logger.info(cardNumber + "有效时间修改为" + mPosChargeData.getValidTimeStart());
										}
										int minitus = (int) Math.ceil((posChargeData.getExitDate().getTime()
												- posChargeData.getEntranceDate().getTime()) / (60 * 1000f));
										if (park.getDescription().contains("子停车场最高优惠分钟数")&&minitus>mianfeifenzhongshu&&mPosChargeData.getParkDesc().contains("临停")) {
											minitus=mianfeifenzhongshu;
										}
										nowTime.add(Calendar.MINUTE, minitus);
										mPosChargeData.setValidTimeStart(sdf.format(nowTime.getTime()));
										logger.info(cardNumber + "修改母停车场" + mPosChargeId + "为" + nowTime.getTime()
												+ "减去分钟数" + minitus);
										logger.info("子母停车场" + mPosChargeData);
										chargeSerivce.updateById(mPosChargeData.getId(), sdf.format(nowTime.getTime()));
									}
								}
							}
						} catch (Exception e) {
							// TODO: handle exception
							logger.info(cardNumber + e.getMessage());
						}
					}

					if (park.getDescription() != null && park.getDescription().contains("包月车子停车场免费")) {
						String[] descriptions3 = park.getDescription().split("；");
						if (descriptions3.length == 1) {
							descriptions3 = park.getDescription().split(";");
						}
						String parkidStr = "";
						for (String stringd : descriptions3) {
							if (stringd.contains("包月车子停车场免费")) {
								parkidStr = stringd.split("-")[1];
							}
						}
						try {
							if ((posChargeData.getParkDesc().contains("包月车")|| park.getDescription().contains("包月车子停车场免费"))) {
								if (StringUtils.isNotBlank(parkidStr) && StringUtils
										.isNotBlank(jedisClient.get("P-" + parkidStr + "-" + cardNumber))) {
									int mPosChargeId = Integer
											.parseInt(jedisClient.get("P-" + parkidStr + "-" + cardNumber));
									logger.info(cardNumber + " 查找到母停车场id " + parkidStr + "poschargeId " + mPosChargeId);
									PosChargeData mPosChargeData = chargeSerivce.getById(mPosChargeId);
									if (mPosChargeData.getId() < posChargeData.getId()) {
										Date newEntranceTime = new Date(mPosChargeData.getEntranceDate().getTime()
												+ posChargeData.getExitDate().getTime()
												- posChargeData.getEntranceDate().getTime());
										if (newEntranceTime.getTime() < System.currentTimeMillis()) {

											// mPosChargeData.setEntranceDate1(newEntranceTime);
											SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
											Calendar nowTime = Calendar.getInstance();
											if (StringUtils.isBlank(mPosChargeData.getValidTimeStart())) {
												nowTime.setTime(mPosChargeData.getEntranceDate());
											} else {
												logger.info(
														cardNumber + "有效时间修改为" + mPosChargeData.getValidTimeStart());
												nowTime.setTime(sdf.parse(mPosChargeData.getValidTimeStart()));
											}
											int minitus = (int) Math.ceil((posChargeData.getExitDate().getTime()
													- posChargeData.getEntranceDate().getTime()) / (60 * 1000f));
											if (park.getDescription().contains("子停车场最高优惠分钟数")&&minitus>mianfeifenzhongshu&&mPosChargeData.getParkDesc().contains("临停")) {
												minitus=mianfeifenzhongshu;
											}
											nowTime.add(Calendar.MINUTE, minitus);
											mPosChargeData.setValidTimeStart(sdf.format(nowTime.getTime()));
											logger.info(cardNumber + "修改母停车场" + mPosChargeId + "为" + nowTime.getTime()
													+ "减去分钟数" + minitus);
											logger.info("子母停车场" + mPosChargeData);
											// chargeSerivce.update(mPosChargeData);
											chargeSerivce.updateById(mPosChargeData.getId(),
													sdf.format(nowTime.getTime()));
										}
									}
								}
							}
						} catch (Exception e) {
							// TODO: handle exception
							logger.info(cardNumber + e.getMessage());
						}
					}

					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 超过了15分钟 小于2天
				else if (diff < 1000 * 60 * 60 * 6 && posChargeData.getPayType() != 2 && posChargeData.getPayType() != 9
						&& posChargeData.getPayType() != 20) {
					logger.info(cardNumber + "支付完后超过15分钟离场,将重新生成计费记录");
					// 超过15分钟 原纪录离场时间要更新
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(payDate);
						chargeSerivce.update(posChargeData);
					}
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(park.getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setIsLargeCar(posChargeData.getIsLargeCar());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payDate));
					charge2.setValidTimeStart(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);
					jedisClient.set("P-" + parkId + "-" + charge2.getCardNumber(), String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);

					logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
					parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
					logger.info(cardNumber + "离开" + park.getName() + "后车位数" + park.getPortLeftCount() + 1);

					jedisClient.del("P-" + parkId + "-" + cardNumber);
					jedisClient.del("query" + cardNumber);
					logger.info("支付完超15分钟重新入场 " + cardNumber + " id" + charge2.getId() + "入场时间"
							+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					jedisClient.set("P-" + parkId + "-" + cardNumber, String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);
					if (num != 1) {
						logger.info(cardNumber + "计费小于2天" + dataMap.toString());
						return Utility.createJsonMsgWithoutMsg(1003, dataMap);
					}
					// 重新查询未缴费

					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				}
				// 如果大于2天,费用归0
				else {
					logger.info(cardNumber + "离上次计费超过两天,没有找到入场记录 判断为异常,免费放行");
					dataMap.put("my", "0.0");
					// 无入场记录 停车时长大于2天 强行按照13分钟处理
					dataMap.put("eD", "13");
					// if (posChargeData.getExitDate() == null) {
					// posChargeData.setExitDate1(new Date());
					// }
					// chargeSerivce.update(posChargeData);
					if (park.getDescription().contains("人工处理")) {
						dataMap.put("aT", "0");
						dataMap.put("uT", "101");
						PosChargeData posCharge = new PosChargeData();
						posCharge.setCardNumber("苏A99999");
						posCharge.setEntrance(false);
						posCharge.setPayType(11);
						posCharge.setEntranceDate(new Date());
						ActiveMqService.SendTopicWithMac(posCharge, String.valueOf(parkId), mac,
								park.getPortLeftCount(), monthUserType);

					}
					logger.info(cardNumber + "计费大于2天" + dataMap.toString());
					carChannelRecordService.carOutRecord(park, posChargeData, args, info, (byte) 2);
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 发送到队列
				if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true) {
					Map<String, String> argstoali = new HashMap<>();
					try {
						argstoali.put("parkName", posChargeData.getParkDesc());
						argstoali.put("carNumber", posChargeData.getCardNumber());
						argstoali.put("enterTime",
								new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(posChargeData.getEntranceDate()));
						argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
								posChargeData.getExitDate() == null ? new Date() : posChargeData.getExitDate()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
					} catch (Exception e) {
						logger.info(cardNumber + "weixinoutinfo：" + e.getMessage());
					}
				}

			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(cardNumber + "计费完毕后处理");// 这段代码什么意思要讨论下
			int num = 0;
			if (!queryCharges.isEmpty()) {
				for (PosChargeData posChargeData : queryCharges) {
					if (posChargeData.getParkId() == parkId.intValue()) {
						posChargeData.setPosId(posChargeData.getPosId() + "-" + macName);
						posChargeData.setExitMacName(macName);
						posChargeData.setExitMac(mac);
						posChargeData.setPaidCompleted(true);
						posChargeData.setPayTime(new SimpleDateFormat(Constants.DATEFORMAT).format(new Date()));
						if (posChargeData.getDiscountType() != null && posChargeData.getDiscountType() == 0) {
							posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						}
						posChargeData.setUnPaidMoney(0);

						if (posChargeData.getPayType() < 20) { // 确保储值卡用户的支付类型不被修改
							posChargeData.setPayType(9);
						}

						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}
						logger.info("posChargeData:"+posChargeData);
						chargeSerivce.update(posChargeData);
					} else {
						posChargeData.setChargeMoney(0.0);
						posChargeData.setPaidCompleted(true);
						posChargeData.setPayTime(new SimpleDateFormat(Constants.DATEFORMAT).format(new Date()));
						if (posChargeData.getPayType() < 20) { // 确保储值卡用户的支付类型不被修改
							posChargeData.setPayType(9);
						}
						chargeSerivce.update(posChargeData);
					}
				}

				shouldChargeMoney = payRet.getChargeMoney();
				logger.info("payRet:"+payRet);
				num = chargeSerivce.update(payRet);
			} else {
				logger.info(cardNumber + "查询计费结果为空!!!");
				dataMap.put("aT", "1");
				dataMap.put("cT", "out");
				dataMap.put("eD", "13");
				dataMap.put("my", "0.0");
				dataMap.put("pT", "9");
				dataMap.put("uT", "0");
				carChannelRecordService.carOutRecord(park, payRet, args, info, (byte) 2);
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			}

			logger.info(cardNumber + "\t" + monthUserType);
			switch (monthUserType) {
			case 0:
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");
				}
				if(charge.getParkDesc().contains("临停车")) {
					dataMap.put("aT", "0");
				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					if (payRet.getChargeMoney() > 0) {
						dataMap.put("aT", "0");
					} else {
						dataMap.put("aT", "1");
					}

				}
				break;
			case 2:
				if (!charge.getParkDesc().contains("储值卡")) {
					dataMap.put("aT", "0");
				}
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (parkcarauthority.getTemporary() != true && shouldChargeMoney > 0) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority.getTemporary0() != true && shouldChargeMoney < 0.1) {
					dataMap.put("aT", "0");
				}

				if (parkcarauthority2 != null) {
					parkcarauthority2
							.setCount((parkcarauthority2.getCount() - 1) < 0 ? 0 : (parkcarauthority2.getCount() - 1));
					parkcarauthority2Service.updateByPrimaryKeySelective(parkcarauthority2);
				}

				break;
			default:
				break;
			}

			logger.info("11"+"\t"+dataMap.get("aT"));
			// chargeSerivce.update(charge);
			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
					|| park.getDescription().contains("本地计费")) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(parkId), mac, park.getPortLeftCount(),
						monthUserType);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, parkId.intValue())
					|| park.getDescription().contains("工行停车场")) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}
			// 如果储值卡有钱 则放行
			if (payRet.getPayType() == 20 && !monthusers.isEmpty() && monthusers.get(0).getPayment() > 0) {
				logger.info(cardNumber + "储值卡 放行");
				dataMap.put("aT", "1");
			}

			if ((!monthusers.isEmpty()) && monthusers.get(0).getType() == 2) {
				dataMap.put("uT", "3");
				monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
				dataMap.put("yue", String.valueOf(monthusers.get(0).getPayment()));
			}

			if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true && !queryCharges.isEmpty()) {
				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getEntranceDate()));
					argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getExitDate()));
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(cardNumber + "发送微信出场通知错误：" + e.getMessage());
				}
			}

			if (payRet.getEntranceDate() == null) {
				return Utility.createJsonMsgWithoutMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());
			if (diff2 < 0) {
				diff2 = 0;
			}

			dataMap.put("eD", String.valueOf((int) Math.ceil((diff2 / (60 * 1000)))));
			if (!isRealMonthUser) {
				dataMap.put("my", String.valueOf(getMoney(payRet)));
			}
			if (isMultiCarsOneCarport && payRet.getParkDesc().contains("包月转临停")) {
				if (payRet.getPaidMoney() > 0) {
					dataMap.put("my", String.valueOf(getMoney(payRet)));
					dataMap.put("uT", "0");
					dataMap.put("aT", "0");
				}
			}
			if (payRet.getPaidMoney() < 0.1) {
				dataMap.put("aT", "1");
			}

			dataMap.put("pT", String.valueOf(payRet.getPayType()));

			if (num == 1) {
				if (isRealMonthUser && isMultiCarsOneCarport) {
					for (Monthuser tmpMonthuser : monthusers) {
						if (tmpMonthuser.getType() == 0 && tmpMonthuser.getPlatecolor() != null
								&& tmpMonthuser.getPlatecolor().equals("多车包月入场")) {
							tmpMonthuser.setPlatecolor("出场完结");
							monthUserService.updateByPrimaryKey(tmpMonthuser);
						}
					}
				}

				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());

				if((!queryCharges.isEmpty()) && (queryCharges.get(0).getParkDesc().contains("临停车"))) {
					dataMap.put("uT", "0");
					dataMap.put("my", String.valueOf(getMoney(payRet)));
				}
				if (park.getDescription() != null && park.getDescription().contains("母停车场")) {
					String[] descriptions = park.getDescription().split("；");
					if (descriptions.length == 1) {
						descriptions = park.getDescription().split(";");
					}
					String parkidStr = "";
					for (String stringd : descriptions) {
						if (stringd.contains("母停车场")) {
							parkidStr = stringd.split("-")[1];
						}
					}
					try {
						if (StringUtils.isNotBlank(parkidStr)
								&& StringUtils.isNotBlank(jedisClient.get("P-" + parkidStr + "-" + cardNumber))) {
							int mPosChargeId = Integer.parseInt(jedisClient.get("P-" + parkidStr + "-" + cardNumber));
							logger.info(cardNumber + " 查找到母停车场id " + parkidStr + "poschargeId " + mPosChargeId);
							PosChargeData mPosChargeData = chargeSerivce.getById(mPosChargeId);
							if (mPosChargeData.getId() < payRet.getId()) {
								Date newEntranceTime = new Date(mPosChargeData.getEntranceDate().getTime()
										+ payRet.getExitDate().getTime() - payRet.getEntranceDate().getTime());
								if (newEntranceTime.getTime() < System.currentTimeMillis()) {

									// mPosChargeData.setEntranceDate1(newEntranceTime);
									SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
									Calendar nowTime = Calendar.getInstance();
									if (StringUtils.isBlank(mPosChargeData.getValidTimeStart())) {
										nowTime.setTime(mPosChargeData.getEntranceDate());
									} else {
										logger.info(cardNumber + "有效时间修改为" + mPosChargeData.getValidTimeStart());
										nowTime.setTime(sdf.parse(mPosChargeData.getValidTimeStart()));
									}
									int minitus = (int) Math
											.ceil((payRet.getExitDate().getTime() - payRet.getEntranceDate().getTime())
													/ (60 * 1000f));
									if (park.getDescription().contains("子停车场最高优惠分钟数")&&minitus>mianfeifenzhongshu&&mPosChargeData.getParkDesc().contains("临停")) {
										minitus=mianfeifenzhongshu;
									}
									nowTime.add(Calendar.MINUTE, minitus);
									mPosChargeData.setValidTimeStart(sdf.format(nowTime.getTime()));
									logger.info(cardNumber + "修改母停车场" + mPosChargeId + "为" + nowTime.getTime() + "减去分钟数"
											+ minitus);
									logger.info("子母停车场" + mPosChargeData);
									chargeSerivce.updateById(mPosChargeData.getId(), sdf.format(nowTime.getTime()));
								}
							}
						}
					} catch (Exception e) {
						// TODO: handle exception
						logger.info(cardNumber + e.getMessage());
					}
				}

				if (park.getDescription() != null && park.getDescription().contains("包月车子停车场免费")) {
					String[] descriptions = park.getDescription().split("；");
					if (descriptions.length == 1) {
						descriptions = park.getDescription().split(";");
					}
					String parkidStr = "";
					for (String stringd : descriptions) {
						if (stringd.contains("包月车子停车场免费")) {
							parkidStr = stringd.split("-")[1];
						}
					}
					try {
						if ((payRet.getParkDesc().contains("包月车")||park.getDescription().contains("子停车场最高优惠分钟数"))) {
							if (StringUtils.isNotBlank(parkidStr)
									&& StringUtils.isNotBlank(jedisClient.get("P-" + parkidStr + "-" + cardNumber))) {
								int mPosChargeId = Integer
										.parseInt(jedisClient.get("P-" + parkidStr + "-" + cardNumber));
								logger.info(cardNumber + " 查找到母停车场id " + parkidStr + "poschargeId " + mPosChargeId);
								PosChargeData mPosChargeData = chargeSerivce.getById(mPosChargeId);
								if (mPosChargeData.getId() < payRet.getId()) {
									Date newEntranceTime = new Date(mPosChargeData.getEntranceDate().getTime()
											+ payRet.getExitDate().getTime() - payRet.getEntranceDate().getTime());
									if (newEntranceTime.getTime() < System.currentTimeMillis()) {

										SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
										Calendar nowTime = Calendar.getInstance();
										if (StringUtils.isBlank(mPosChargeData.getValidTimeStart())) {
											nowTime.setTime(mPosChargeData.getEntranceDate());
										} else {
											logger.info(cardNumber + "有效时间修改为" + mPosChargeData.getValidTimeStart());
											nowTime.setTime(sdf.parse(mPosChargeData.getValidTimeStart()));
										}
										int minitus = (int) Math.ceil(
												(payRet.getExitDate().getTime() - payRet.getEntranceDate().getTime())
														/ (60 * 1000f));
										if (park.getDescription().contains("子停车场最高优惠分钟数")&&minitus>mianfeifenzhongshu&&mPosChargeData.getParkDesc().contains("临停")) {
											minitus=mianfeifenzhongshu;
										}
										nowTime.add(Calendar.MINUTE, minitus);
										mPosChargeData.setValidTimeStart(sdf.format(nowTime.getTime()));
										logger.info(cardNumber + "修改母停车场" + mPosChargeId + "为" + nowTime.getTime()
												+ "减去分钟数" + minitus);
										logger.info("子母停车场" + mPosChargeData);
										chargeSerivce.updateById(mPosChargeData.getId(), sdf.format(nowTime.getTime()));
									}
								}
							}
						}
					} catch (Exception e) {
						// TODO: handle exception
						logger.info(cardNumber + e.getMessage());
					}
				}

				jedisClient.set("out" + mac, String.valueOf(payRet.getId()));

				if (park.getDescription() != null && park.getDescription().contains("子停车场包月免费")) {
					String[] descriptions = park.getDescription().split("；");
					if (descriptions.length == 1) {
						descriptions = park.getDescription().split(";");
					}
					String parkidStr = "";
					for (String stringd : descriptions) {
						if (stringd.contains("子停车场包月免费")) {
							parkidStr = stringd.split("-")[1];
						}
					}
					try {
						if (payRet.getParkDesc().contains("包月车")) {
							if (StringUtils.isNotBlank(parkidStr)
									&& StringUtils.isNotBlank(jedisClient.get("P-" + parkidStr + "-" + cardNumber))) {
								int mPosChargeId = Integer
										.parseInt(jedisClient.get("P-" + parkidStr + "-" + cardNumber));
								logger.info(cardNumber + " 查找到母停车场id " + parkidStr + "poschargeId " + mPosChargeId);
								PosChargeData mPosChargeData = chargeSerivce.getById(mPosChargeId);
								if (mPosChargeData.getId() < payRet.getId()) {
									Date newEntranceTime = new Date(mPosChargeData.getEntranceDate().getTime()
											+ payRet.getExitDate().getTime() - payRet.getEntranceDate().getTime());
									if (newEntranceTime.getTime() < System.currentTimeMillis()) {
										logger.info(cardNumber + "修改母停车场" + mPosChargeId + "入场时间由"
												+ Utility.formatDatetime(mPosChargeData.getEntranceDate()) + "为"
												+ Utility.formatDatetime(newEntranceTime));
//										mPosChargeData.setEntranceDate1(newEntranceTime);
										SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
										Calendar nowTime = Calendar.getInstance();
										if (StringUtils.isBlank(mPosChargeData.getValidTimeStart())) {
											nowTime.setTime(mPosChargeData.getEntranceDate());
										} else {
											nowTime.setTime(sdf.parse(mPosChargeData.getValidTimeStart()));
										}
										nowTime.add(Calendar.MINUTE, (int) Math.ceil(
												(payRet.getExitDate().getTime() - payRet.getEntranceDate().getTime())
														/ 60 / 1000));
										mPosChargeData.setValidTimeStart(sdf.format(nowTime.getTime()));
										logger.info("子母停车场" + mPosChargeData);
										chargeSerivce.updateById(mPosChargeData.getId(), sdf.format(nowTime.getTime()));
									}
								}
							}
						}
					} catch (Exception e) {
						// TODO: handle exception
						logger.info(cardNumber + e.getMessage());
					}
				}
				jedisClient.set("out" + mac, String.valueOf(payRet.getId()));
				logger.info(cardNumber + "出场成功!" + dataMap.toString());
				carChannelRecordService.carOutRecord(park, payRet, args, info, (byte) 1);
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			} else {
				carChannelRecordService.carOutRecord(park, payRet, args, info, (byte) 2);
				return Utility.createJsonMsgWithoutMsg(1001, "ok");
			}
		}
	}

	@RequestMapping(value = "touchedDetail", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public String touchedDetail(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		cardNumber = cardNumber.trim();
		logger.info("[touchedDetail]进入车辆入场/出场接口,车牌号： " + cardNumber);

		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		PosChargeData charge = new PosChargeData();
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1003);
			return Utility.gson.toJson(dataMap);
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			dataMap.put("aT", "1");
			dataMap.put("cT", "in");
			dataMap.put("uT", "0");
			return Utility.createJsonMsgWithoutMsg(1001, dataMap);
		}
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		// 相似车牌管理
		List<Similarcarnumber> similarcarnumbers = similarCarNumberService.selectBySimilarCarNumberAndPark(cardNumber,
				parkId);
		if (!similarcarnumbers.isEmpty()) {
			cardNumber = similarcarnumbers.get(0).getRealnumber();
			logger.info("更正车牌:" + cardNumber);
		}

		List<Monthuser> monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);// 包月用户信息
		Park park = parkService.getParkById(parkId);
		List<Parkcarauthority> parkcarauthorities = parkCarAuthorityService.getByParkId(parkId);
		if (parkcarauthorities.isEmpty()) {
			return null;
		}

		dataMap.put("aT", "1");

		boolean isMonthUser = false;
		boolean isRealMonthUser = false;
		int monthUserType = 9;
		Monthuser monthuserNow = null;
		if (!monthusers.isEmpty()) {
			isMonthUser = true;
			for (Monthuser tmpMonthuser : monthusers) {
				if (tmpMonthuser.getType() == 17) {
					dataMap.put("aT", "0");
					dataMap.put("uT", "17");
					dataMap.put("cT", "in");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				logger.info(cardNumber + "月卡id:" + tmpMonthuser.getId() + "类型:" + tmpMonthuser.getType() + "到期时间:"
						+ Utility.formatDatetime(tmpMonthuser.getEndtime()));
				if (park.getDescription().contains("月卡7天延期")) {
					tmpMonthuser.setEndtime1(new Date(tmpMonthuser.getEndtime().getTime() + 7 * 24 * 60 * 60 * 1000));
				}
				Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
				monthuserNow = tmpMonthuser;
				boolean isVip = tmpMonthuser.getType() >= 9 && tmpMonthuser.getType() <= 13;
				if (tmpMonthuser.getType() == 0 || isVip) {// 月卡
					monthUserType = 0;
					if (diff > 0) {
						int leftDays = (int) (diff / (1000 * 60 * 60 * 24));// 月卡到期时间
						dataMap.put("ds", String.valueOf(leftDays));
						dataMap.put("my", "0.0");
						isRealMonthUser = true;
						dataMap.put("eD", new SimpleDateFormat(Constants.DATEFORMAT).format(tmpMonthuser.getEndtime()));
						break;
					} else {
						dataMap.put("ds", "-1");
						dataMap.put("uT", "1");
						monthUserType = 8; // 月卡过期

					}

				} else {
					if (diff >= 0) {
						monthUserType = tmpMonthuser.getType();// 月卡类型
						break;
					} else {
						isMonthUser = false;
					}
				}
			}
		}

		if (!isMonthUser) {// ??
			dataMap.put("uT", "0");
		} else {
			dataMap.put("uT", "1");
			// 判断是否是预约车
			if (monthUserType == 1) {
				dataMap.put("uT", "2");
			}
		}

		if (!monthusers.isEmpty() && monthusers.get(0).getType() == 2) {
			dataMap.put("uT", "3");
			dataMap.put("yue", String.valueOf(monthusers.get(0).getPayment()));
		}

		List<Parktoalipark> parktoaliparks = parkToAliparkService.getByParkId(parkId);// ??
		Boolean isMultiCarsOneCarport = false;
		if (park.getDescription() != null && park.getDescription().contains("一位多车")) {
			logger.info(cardNumber + "进入一位多车停车场");
			isMultiCarsOneCarport = true;
		}
		Boolean isMultiFeeCtriterion = false;
		if (park.getDescription() != null && park.getDescription().contains("多计费标准")) {
			isMultiFeeCtriterion = true;
		}
		// 判断是否有多个车
		List<Monthuser> realMonthUsers = new ArrayList<>();
		if (monthuserNow != null && monthuserNow.getCardnumber() != null
				&& !monthuserNow.getCardnumber().trim().equals("")) {
			List<Monthuser> monthuserss = monthUserService.getByParkAndPort(monthuserNow.getParkid(),
					monthuserNow.getCardnumber());
			for (Monthuser monthuser : monthuserss) {
				logger.info(cardNumber + "多个车" + monthuser.getId());
				if (monthuser.getType().intValue() == 0) {
					realMonthUsers.add(monthuser);
				}
			}
		}

		Parknoticeauthority parknoticeauthority = parkNoticeAuthorityService.getByParkId(parkId).get(0);
		Parkcarauthority2 parkcarauthority2 = parkcarauthority2Service.selectByPark(parkId);

		charge.setParkDesc(park.getName() + "-临停车");

		// 入口硬件
		if (channelFlag == 1) {
			dataMap.put("lT", String.valueOf(park.getPortLeftCount()));
			if (largeCar == true) {
				charge.setIsLargeCar(true);
			}
			logger.info(cardNumber + "--->开始入场，mac" + mac);
			// 取得入口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 1) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			// 贵州队列发送
			if (parkId == 334) {
				Map<String, String> guiyang = new HashMap<>();
				guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
				guiyang.put("type", "in");
				guiyang.put("plateNum", cardNumber);
				guiyang.put("enterTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
			}

			dataMap.put("cT", "in");
			charge.setCardNumber(cardNumber);
			charge.setParkId(parkId);

			// 判断包月车已入场

			Boolean isMonthUserCarIn = false;
			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				for (Monthuser tmMonthuser : realMonthUsers) {
					if (tmMonthuser.getPlatecolor() != null && tmMonthuser.getPlatecolor().equals("多车包月入场")) {
						logger.info(cardNumber + "同车位已经入场Id" + tmMonthuser.getId());
						if (!tmMonthuser.getPlatenumber().equals(cardNumber)) {
							isMonthUserCarIn = true;
						}

					}
				}
				monthuserNow.setPlatecolor("多车包月入场");
				if (isMonthUserCarIn) {
					charge.setParkDesc(park.getName() + "-包月转临停");
					monthuserNow.setPlatecolor("包月转临停");
					dataMap.put("uT", "0");
					monthUserType = 9;
					try {
						dataMap.remove("ds");
						dataMap.remove("eD");
					} catch (Exception e) {
						// TODO: handle exception
					}

				}
				monthUserService.updateByPrimaryKeySelective(monthuserNow);
			}

			switch (monthUserType) {
			case 0:
				charge.setParkDesc(park.getName() + "-包月车");
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");
					dataMap.put("uT", "1");
				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");
					dataMap.put("uT", "2");
				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (!charge.getParkDesc().contains("包月转临停")) {
					charge.setParkDesc(park.getName() + "-临停车");
				}

				if (parkcarauthority.getTemporary() != true) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority2 != null) {
					logger.info("临停数量" + parkcarauthority2.getCount());
					if (parkcarauthority2.getCount() >= parkcarauthority2.getMaxcount()) {
						dataMap.put("aT", "0");
						logger.info(park.getId() + "-" + cardNumber + "-临停数量达到限制,无法入场");
					} else {
						parkcarauthority2.setCount(parkcarauthority2.getCount() + 1);
						parkcarauthority2Service.updateByPrimaryKey(parkcarauthority2);
					}
				}
				break;
			default:
				break;
			}

			if (!monthusers.isEmpty()) {
				charge.setRejectReason(monthusers.get(0).getOwner());
			}
			charge.setPosId(mac);
			charge.setExitMacName(mac);
			
			charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			int num = 0;
			// redis查询
			try {
				String redisStr = jedisClient.get("carIn" + parkId + charge.getCardNumber());
				if (redisStr != null) {
					num = 1;
					logger.info("redis检测重复入场" + cardNumber + redisStr);
				} else {

					// 12小时重复检测
					if (park.getDescription().contains("12小时内免费")
							&& jedisClient.get("P-" + park.getId() + "-12-" + cardNumber) != null) {
						logger.info(cardNumber + "12小时内免费");
						charge.setOther2(jedisClient.get("P-" + park.getId() + "-12-" + cardNumber));
					}

					num = chargeSerivce.insert(charge);
					// 清场
					String redisId = jedisClient.get("P-" + park.getId() + "-" + cardNumber);
					if (redisId != null) {
						PosChargeData posChargeDatatmp = chargeDao.getById(Integer.parseInt(redisId));
						if (posChargeDatatmp != null && !posChargeDatatmp.isPaidCompleted()) {
							posChargeDatatmp.setChargeMoney(0);
							posChargeDatatmp.setExitDate1(posChargeDatatmp.getEntranceDate());
							posChargeDatatmp.setPaidCompleted(true);
							posChargeDatatmp.setPayTime(new SimpleDateFormat(Constants.DATEFORMAT).format(new Date()));
							posChargeDatatmp.setOperatorId("redis");
							posChargeDatatmp.setRejectReason("平台清场r");
							logger.info(cardNumber + "清场" + posChargeDatatmp.getId());
							// chargeSerivce.update(posChargeDatatmp);
							chargeSerivce.deleteById(posChargeDatatmp.getId());
						}

						jedisClient.del("P-" + parkId + "-" + charge.getCardNumber());
						jedisClient.del("query" + charge.getCardNumber());
						logger.info("重新入场 " + cardNumber + " id" + charge.getId());

					}

					jedisClient.set("carIn" + parkId + charge.getCardNumber(), String.valueOf(charge.getId()), 120);
					logger.info(cardNumber + "设置redis值" + charge.getId());
					jedisClient.set("carInTmp" + parkId + charge.getCardNumber(), "tmp", 20);
					jedisClient.set("query" + charge.getCardNumber(), String.valueOf(charge.getId()), 259200);
					jedisClient.set("P-" + parkId + "-" + charge.getCardNumber(), String.valueOf(charge.getId()),
							Constants.carMaxPeriod);

				}
			} catch (Exception e) {
				// TODO: handle exception
				logger.info(cardNumber + "redis异常" + e);
				dataMap.put("aT", "1");
				dataMap.put("cT", "in");
				dataMap.put("uT", "0");
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				// num = chargeSerivce.insert(charge);
			}

			if (num == 1) {

				// 发送topic队列
				if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
						|| park.getDescription().contains("本地计费")) {
					charge.setEntrance(true);
					ActiveMqService.SendTopicWithMac(charge, String.valueOf(parkId), mac, park.getPortLeftCount(),
							monthUserType);
				}

				try {

					if (!parktoaliparks.isEmpty()) {
						Parktoalipark parktoalipark = parktoaliparks.get(0);
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", parktoalipark.getAliparkingid());
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");

					}
					if (parknoticeauthority != null && parknoticeauthority.getAlipay() == true) {
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", "PI1501317472942184881");
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");
					}
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e);
				}
				logger.info(cardNumber + "入场成功!--" + dataMap.toString());
				ret.put("status", 1001);
			} else {
				ret.put("status", 1002);
			}
			return Utility.createJsonMsgWithoutMsg(1001, dataMap);
		} else {// 出场硬件
			logger.info(cardNumber + "--->开始出场，mac " + mac);
			dataMap.put("cT", "out");
			dataMap.put("pT", "9");
			List<PosChargeData> queryCharges = null;
			String exitDate = (String) args.get("exitDate");

			if (jedisClient.get("carInTmp" + parkId + cardNumber) != null) {
				dataMap.put("aT", "2");
				logger.info("一分钟内 不许出场");
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			}
			logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
			int portLeftCount = park.getPortLeftCount() + 1;
			parkService.updateLeftPortCount(parkId, portLeftCount);
			logger.info(cardNumber + "离开" + park.getName() + "后车位数" + portLeftCount);
			if (monthuserNow != null) {
				realMonthUsers.add(monthuserNow);
			}

			// 取得出口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 0) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			if (exitDate != null) {

				try {
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);

				} catch (Exception e) {
					// TODO Auto-generated catch block
					logger.info(cardNumber + "计费异常" + e.getMessage());
					dataMap.put("aT", "1");
					dataMap.put("cT", "out");
					dataMap.put("eD", "13");
					dataMap.put("my", "0.0");
					dataMap.put("pT", "9");
					dataMap.put("uT", "0");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
			} else {
				try {
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);

				} catch (Exception e) {
					logger.info(cardNumber + "计费异常" + e.getMessage());
					dataMap.put("aT", "1");
					dataMap.put("cT", "out");
					dataMap.put("eD", "13");
					dataMap.put("my", "0.0");
					dataMap.put("pT", "9");
					dataMap.put("uT", "0");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
			}
			logger.info(cardNumber + "计费结束!");
			double shouldChargeMoney = 0;
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {
				logger.info(cardNumber + "没有未缴费!");
				FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());
				if (isRealMonthUser && realMonthUsers.size() == 1) {
					logger.info(cardNumber + "月卡用户直接返回!" + dataMap.toString());
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 以下就是查询停车费状态的部分迁移
				logger.info(cardNumber + "开始查询最后一次记录!");
				List<PosChargeData> posChargeDataList = chargeSerivce.getLastRecordWithPark(cardNumber, 1, parkId);
				if (posChargeDataList.isEmpty()) {
					dataMap.put("my", "0.0");
					dataMap.put("eD", "13");
					logger.info(cardNumber + "在此停车场没有任何记录!" + dataMap.toString());
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				PosChargeData posChargeData = posChargeDataList.get(0);
				logger.info(cardNumber + "最后一次记录id" + posChargeData.getId() + "paytype" + posChargeData.getPayType());
				// 支付类型
				dataMap.put("pT", String.valueOf(posChargeData.getPayType()));
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
				}
				Date payDate = posChargeData.getExitDate();
				if (posChargeData.getPayType() == 0 || posChargeData.getPayType() == 1) {
					logger.info(cardNumber + "已经通过移动进行支付");
					List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
					if (alipayrecords.isEmpty()) {
						payDate = new Date();
					} else {
						payDate = alipayrecords.get(0).getDate();
						logger.info(
								cardNumber + "已查询到支付时间!" + new SimpleDateFormat(Constants.DATEFORMAT).format(payDate));
					}

				}
				if (payDate == null) {
					payDate = new Date();
				}
				long diff = new Date().getTime() - payDate.getTime();
				// 防止逃费
				String[] descriptions = park.getDescription().replace("；", ",").split(";");
				String dealyStr = "";
				for (String tmpStr : descriptions) {
					if (tmpStr.contains("防逃费")) {
						dealyStr = tmpStr.split("-")[1];
					}
				}
				Long maxDelay = (long) 12;
				if (StringUtils.isNotBlank(dealyStr)) {
					maxDelay = (long) Integer.parseInt(dealyStr);
				}
				Long second = (long) 1000 * (long) 60 * (long) 60 * maxDelay;
				if ((posChargeData.getPayType() == 9) && (diff < second)) {
					dataMap.put("my", String.valueOf(posChargeData.getPaidMoney()));
					if (posChargeData.getChargeMoney() > 0.1) {
						dataMap.put("aT", "0");
					} else {
						dataMap.put("aT", "1");
					}

					dataMap.put("eD", String.valueOf(Math
							.ceil((new Date().getTime() - posChargeData.getEntranceDate().getTime()) / (1000 * 60))));
					logger.info(cardNumber + "防逃费:" + dataMap.toString());
					jedisClient.set("out" + mac, String.valueOf(posChargeData.getId()));
					// 发送到topic
					if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
							|| park.getDescription().contains("本地计费")) {
						posChargeData.setEntrance(false);
						ActiveMqService.SendTopicWithMac(posChargeData, String.valueOf(parkId), mac,
								park.getPortLeftCount(), monthUserType);
					}
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}

				if (diff < 1000 * 60 * 15) {
					logger.info(cardNumber + "在支付完15分钟内离场");
					dataMap.put("my", "0.0");
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					long diff1 = (posChargeData.getExitDate().getTime() - posChargeData.getEntranceDate().getTime());
					dataMap.put("eD",
							String.valueOf((diff1 / (1000 * 60)) > 15 ? 13 : Math.ceil((diff1 / (1000 * 60)))));

					posChargeData.setPaidCompleted(true);
					posChargeData.setPayTime(new SimpleDateFormat(Constants.DATEFORMAT).format(new Date()));
					posChargeData.setUnPaidMoney(0);
					// if (posChargeData.getExitDate() == null) {
					// //posChargeData.setExitDate1(new Date());
					// posChargeData.setExitDate1(payDate);
					// }
					chargeSerivce.update(posChargeData);
					logger.info(cardNumber + "小于15分钟返回" + dataMap.toString());
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 超过了15分钟 小于2天
				else if (diff < 1000 * 60 * 60 * 6 && posChargeData.getPayType() != 2 && posChargeData.getPayType() != 9
						&& posChargeData.getPayType() < 20) {
					logger.info(cardNumber + "支付完后超过15分钟离场,将重新生成计费记录");
					// 超过15分钟 原纪录离场时间要更新
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(payDate);
						chargeSerivce.update(posChargeData);
					}
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(park.getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);
					jedisClient.set("P-" + parkId + "-" + charge2.getCardNumber(), String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);

					logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
					parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
					logger.info(cardNumber + "离开" + park.getName() + "后车位数" + park.getPortLeftCount() + 1);

					jedisClient.del("P-" + parkId + "-" + cardNumber);
					jedisClient.del("query" + cardNumber);
					logger.info("支付完超15分钟重新入场 " + cardNumber + " id" + charge2.getId() + "入场时间"
							+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					jedisClient.set("P-" + parkId + "-" + cardNumber, String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);
					if (num != 1) {
						logger.info(cardNumber + "计费小于2天" + dataMap.toString());
						return Utility.createJsonMsgWithoutMsg(1003, dataMap);
					}
					// 重新查询未缴费

					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				}
				// 如果大于2天,费用归0
				else {
					logger.info(cardNumber + "离上次计费超过两天,没有找到入场记录 判断为异常,免费放行");
					dataMap.put("my", "0.0");
					// 无入场记录 停车时长大于2天 强行按照13分钟处理
					dataMap.put("eD", "13");
					// if (posChargeData.getExitDate() == null) {
					// posChargeData.setExitDate1(new Date());
					// }
					// chargeSerivce.update(posChargeData);
					logger.info(cardNumber + "计费大于2天" + dataMap.toString());
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 发送到队列
				if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true) {
					Map<String, String> argstoali = new HashMap<>();
					try {
						argstoali.put("parkName", posChargeData.getParkDesc());
						argstoali.put("carNumber", posChargeData.getCardNumber());
						argstoali.put("enterTime",
								new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(posChargeData.getEntranceDate()));
						argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
								posChargeData.getExitDate() == null ? new Date() : posChargeData.getExitDate()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
					} catch (Exception e) {
						logger.info(cardNumber + "weixinoutinfo：" + e.getMessage());
					}
				}

			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(cardNumber + "计费完毕后处理");// 这段代码什么意思要讨论下
			int num = 0;
			if (!queryCharges.isEmpty()) {
				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(mac);
					if (posChargeData.getParkId() == parkId.intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);

						if (posChargeData.getPayType() < 20) { // 确保储值卡用户的支付类型不被修改
							posChargeData.setPayType(9);
						}

						charge.setPosId(mac);
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}
						chargeSerivce.update(posChargeData);
					} else {
						posChargeData.setChargeMoney(0.0);
						posChargeData.setPaidCompleted(true);
						if (posChargeData.getPayType() < 20) { // 确保储值卡用户的支付类型不被修改
							posChargeData.setPayType(9);
						}
						chargeSerivce.update(posChargeData);
					}
				}

				shouldChargeMoney = payRet.getChargeMoney();
				num = chargeSerivce.update(payRet);
			} else {
				logger.info(cardNumber + "查询计费结果为空!!!");
				dataMap.put("aT", "1");
				dataMap.put("cT", "out");
				dataMap.put("eD", "13");
				dataMap.put("my", "0.0");
				dataMap.put("pT", "9");
				dataMap.put("uT", "0");
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			}

			switch (monthUserType) {
			case 0:
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					if (payRet.getChargeMoney() > 0) {
						dataMap.put("aT", "0");
					} else {
						dataMap.put("aT", "1");
					}

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (parkcarauthority.getTemporary() != true && shouldChargeMoney > 0) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority.getTemporary0() != true && shouldChargeMoney < 0.1) {
					dataMap.put("aT", "0");
				}

				if (parkcarauthority2 != null) {
					parkcarauthority2
							.setCount((parkcarauthority2.getCount() - 1) < 0 ? 0 : (parkcarauthority2.getCount() - 1));
					parkcarauthority2Service.updateByPrimaryKeySelective(parkcarauthority2);
				}

				break;
			default:
				break;
			}

			// chargeSerivce.update(charge);

			// 发送到贵州
			if (parkId == 334) {
				Map<String, String> guiyang = new HashMap<>();
				guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
				guiyang.put("type", "out");
				guiyang.put("plateNum", cardNumber);
				guiyang.put("leaveTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
			}
			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
					|| park.getDescription().contains("本地计费")) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(parkId), mac, park.getPortLeftCount(),
						monthUserType);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, parkId.intValue())
					|| park.getDescription().contains("工行停车场")) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}
			// 如果储值卡有钱 则放行
			if (payRet.getPayType() == 20 && !monthusers.isEmpty() && monthusers.get(0).getPayment() > 0) {
				logger.info(cardNumber + "储值卡 放行");
				dataMap.put("aT", "1");
			}

			if ((!monthusers.isEmpty()) && monthusers.get(0).getType() == 2) {
				dataMap.put("uT", "3");
				monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
				dataMap.put("yue", String.valueOf(monthusers.get(0).getPayment()));
			}

			if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true && !queryCharges.isEmpty()) {
				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getEntranceDate()));
					argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getExitDate()));
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(cardNumber + "发送微信出场通知错误：" + e.getMessage());
				}
			}

			if (payRet.getEntranceDate() == null) {
				return Utility.createJsonMsgWithoutMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());

			dataMap.put("eD", String.valueOf(Math.ceil((diff2 / (60 * 1000)))));
			if (!isRealMonthUser) {
				dataMap.put("my", String.valueOf(payRet.getPaidMoney()));
			}
			if (isMultiCarsOneCarport && payRet.getParkDesc().contains("包月转临停")) {
				if (payRet.getChargeMoney() > 0) {
					dataMap.put("my", String.valueOf(payRet.getPaidMoney()));
					dataMap.put("uT", "0");
					dataMap.put("aT", "0");
				}
			}
			if (payRet.getChargeMoney() < 0.1) {
				dataMap.put("aT", "1");
			}

			dataMap.put("pT", String.valueOf(payRet.getPayType()));

			if (num == 1) {
				if (isRealMonthUser && isMultiCarsOneCarport) {
					for (Monthuser tmpMonthuser : monthusers) {
						if (tmpMonthuser.getType() == 0 && tmpMonthuser.getPlatecolor() != null
								&& tmpMonthuser.getPlatecolor().equals("多车包月入场")) {
							tmpMonthuser.setPlatecolor("出场完结");
							monthUserService.updateByPrimaryKey(tmpMonthuser);
						}
					}
				}

				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());

				if (park.getDescription() != null && park.getDescription().contains("母停车场")) {
					String[] descriptions = park.getDescription().split("；");
					if (descriptions.length == 1) {
						descriptions = park.getDescription().split(";");
					}
					String parkidStr = "";
					for (String stringd : descriptions) {
						if (stringd.contains("母停车场")) {
							parkidStr = stringd.split("-")[1];
						}
					}
					try {
						if (StringUtils.isNotBlank(parkidStr)
								&& StringUtils.isNotBlank(jedisClient.get("P-" + parkidStr + "-" + cardNumber))) {
							int mPosChargeId = Integer.parseInt(jedisClient.get("P-" + parkidStr + "-" + cardNumber));
							logger.info(cardNumber + " 查找到母停车场id " + parkidStr + "poschargeId " + mPosChargeId);
							PosChargeData mPosChargeData = chargeSerivce.getById(mPosChargeId);
							if (mPosChargeData.getId() < payRet.getId()) {
								Date newEntranceTime = new Date(mPosChargeData.getEntranceDate().getTime()
										+ payRet.getExitDate().getTime() - payRet.getEntranceDate().getTime());
								if (newEntranceTime.getTime() < System.currentTimeMillis()) {
									logger.info(cardNumber + "修改母停车场" + mPosChargeId + "入场时间由"
											+ Utility.formatDatetime(mPosChargeData.getEntranceDate()) + "为"
											+ Utility.formatDatetime(newEntranceTime));
									mPosChargeData.setEntranceDate1(newEntranceTime);
									chargeSerivce.update(mPosChargeData);
								}
							}
						}
					} catch (Exception e) {
						// TODO: handle exception
						logger.info(cardNumber + e.getMessage());
					}
				}

				jedisClient.set("out" + mac, String.valueOf(payRet.getId()));
				logger.info(cardNumber + "出场成功!" + dataMap.toString());

				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			} else {
				return Utility.createJsonMsgWithoutMsg(1001, "ok");
			}
		}
	}

	@RequestMapping(value = "testTouched", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String testTouched(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		cardNumber = cardNumber.trim();
		logger.info("[touched]进入车辆入场/出场接口,车牌号： " + cardNumber);

		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		PosChargeData charge = new PosChargeData();
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1003);
			return Utility.gson.toJson(dataMap);
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			dataMap.put("aT", "1");
			dataMap.put("cT", "in");
			dataMap.put("uT", "0");
			return Utility.createJsonMsg(1001, dataMap);
		}
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		// 相似车牌管理
		List<Similarcarnumber> similarcarnumbers = similarCarNumberService.selectBySimilarCarNumberAndPark(cardNumber,
				parkId);
		if (!similarcarnumbers.isEmpty()) {
			cardNumber = similarcarnumbers.get(0).getRealnumber();
			logger.info("更正车牌:" + cardNumber);
		}

		List<Monthuser> monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
		Park park = parkService.getParkById(parkId);
		List<Parkcarauthority> parkcarauthorities = parkCarAuthorityService.getByParkId(parkId);
		if (parkcarauthorities.isEmpty()) {
			return null;
		}

		dataMap.put("aT", "1");

		boolean isMonthUser = false;
		boolean isRealMonthUser = false;
		int monthUserType = 9;
		Monthuser monthuserNow = null;
		if (!monthusers.isEmpty()) {
			isMonthUser = true;
			for (Monthuser tmpMonthuser : monthusers) {
				logger.info(cardNumber + "月卡id:" + tmpMonthuser.getId() + "类型:" + tmpMonthuser.getType() + "到期时间:"
						+ Utility.formatDatetime(tmpMonthuser.getEndtime()));
				Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
				monthuserNow = tmpMonthuser;
				if (tmpMonthuser.getType() == 0) {// 月卡
					monthUserType = 0;
					if (diff > 0) {
						int leftDays = (int) (diff / (1000 * 60 * 60 * 24));
						dataMap.put("ds", String.valueOf(leftDays));
						isRealMonthUser = true;
						dataMap.put("my", "0.0");
						dataMap.put("eD", new SimpleDateFormat(Constants.DATEFORMAT).format(tmpMonthuser.getEndtime()));
						break;
					} else {
						dataMap.put("ds", "-1");
						dataMap.put("uT", "1");
						monthUserType = 8; // 月卡过期

					}

				} else {
					if (diff >= 0) {
						monthUserType = tmpMonthuser.getType();
						break;
					} else {
						isMonthUser = false;
					}
				}
			}
		}

		if (!isMonthUser) {
			dataMap.put("uT", "0");
		} else {
			dataMap.put("uT", "1");
			// 判断是否是预约车
			if (monthUserType == 1) {
				dataMap.put("uT", "2");
			}
		}

		if (!monthusers.isEmpty() && monthusers.get(0).getType() == 2) {
			dataMap.put("uT", "3");
			dataMap.put("yue", String.valueOf(monthusers.get(0).getPayment()));
		}

		List<Parktoalipark> parktoaliparks = parkToAliparkService.getByParkId(parkId);
		Boolean isMultiCarsOneCarport = false;
		if (park.getDescription() != null && park.getDescription().contains("一位多车")) {
			logger.info(cardNumber + "进入一位多车停车场");
			isMultiCarsOneCarport = true;
		}
		Boolean isMultiFeeCtriterion = false;
		if (park.getDescription() != null && park.getDescription().contains("多计费标准")) {
			isMultiFeeCtriterion = true;
		}
		// 判断是否有多个车
		List<Monthuser> realMonthUsers = new ArrayList<>();
		if (monthuserNow != null && monthuserNow.getCardnumber() != null
				&& !monthuserNow.getCardnumber().trim().equals("")) {
			List<Monthuser> monthuserss = monthUserService.getByParkAndPort(monthuserNow.getParkid(),
					monthuserNow.getCardnumber());
			for (Monthuser monthuser : monthuserss) {
				logger.info(cardNumber + "多个车" + monthuser.getId());
				if (monthuser.getType().intValue() == 0) {
					realMonthUsers.add(monthuser);
				}
			}
		}

		Parknoticeauthority parknoticeauthority = parkNoticeAuthorityService.getByParkId(parkId).get(0);
		Parkcarauthority2 parkcarauthority2 = parkcarauthority2Service.selectByPark(parkId);

		charge.setParkDesc(park.getName() + "-临停车");

		// 入口硬件
		if (channelFlag == 1) {
			if (largeCar == true) {
				charge.setIsLargeCar(true);
			}
			logger.info(cardNumber + "--->开始入场");
			// 取得入口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 1) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			// 贵州队列发送
			if (parkId == 334) {
				Map<String, String> guiyang = new HashMap<>();
				guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
				guiyang.put("type", "in");
				guiyang.put("plateNum", cardNumber);
				guiyang.put("enterTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
			}

			dataMap.put("cT", "in");
			charge.setCardNumber(cardNumber);
			charge.setParkId(parkId);

			// 判断包月车已入场

			Boolean isMonthUserCarIn = false;
			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				for (Monthuser tmMonthuser : realMonthUsers) {
					if (tmMonthuser.getPlatecolor() != null && tmMonthuser.getPlatecolor().equals("多车包月入场")) {
						logger.info(cardNumber + "同车位已经入场Id" + tmMonthuser.getId());
						if (!tmMonthuser.getPlatenumber().equals(cardNumber)) {
							isMonthUserCarIn = true;
						}

					}
				}
				monthuserNow.setPlatecolor("多车包月入场");
				if (isMonthUserCarIn) {
					charge.setParkDesc(park.getName() + "-包月转临停");
					monthuserNow.setPlatecolor("包月转临停");
					dataMap.put("uT", "0");
					monthUserType = 9;
					try {
						dataMap.remove("ds");
						dataMap.remove("eD");
					} catch (Exception e) {
						// TODO: handle exception
					}

				}
				monthUserService.updateByPrimaryKeySelective(monthuserNow);
			}

			switch (monthUserType) {
			case 0:
				charge.setParkDesc(park.getName() + "-包月车");
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (!charge.getParkDesc().contains("包月转临停")) {
					charge.setParkDesc(park.getName() + "-临停车");
				}

				if (parkcarauthority.getTemporary() != true) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority2 != null) {
					logger.info("临停数量" + parkcarauthority2.getCount());
					if (parkcarauthority2.getCount() >= parkcarauthority2.getMaxcount()) {
						dataMap.put("aT", "0");
						logger.info(park.getId() + "-" + cardNumber + "-临停数量达到限制,无法入场");
					} else {
						parkcarauthority2.setCount(parkcarauthority2.getCount() + 1);
						parkcarauthority2Service.updateByPrimaryKey(parkcarauthority2);
					}
				}
				break;
			default:
				break;
			}
			if (!monthusers.isEmpty()) {
				charge.setRejectReason(monthusers.get(0).getOwner());
			}
			charge.setPosId(mac);
			charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			int num = 0;
			// redis查询
			try {
				String redisStr = jedisClient.get("carIn" + parkId + charge.getCardNumber());
				if (redisStr != null) {
					num = 1;
					logger.info("redis检测重复入场" + cardNumber + redisStr);
				} else {
					num = chargeSerivce.insert(charge);
					// 清场
					String redisId = jedisClient.get("P-" + park.getId() + "-" + cardNumber);
					if (redisId != null) {
						PosChargeData posChargeDatatmp = chargeDao.getById(Integer.parseInt(redisId));
						if (posChargeDatatmp != null && !posChargeDatatmp.isPaidCompleted()) {
							posChargeDatatmp.setChargeMoney(0);
							posChargeDatatmp.setExitDate1(posChargeDatatmp.getEntranceDate());
							posChargeDatatmp.setPaidCompleted(true);
							posChargeDatatmp.setOperatorId("redis");
							posChargeDatatmp.setRejectReason("平台清场r");
							logger.info(cardNumber + "清场" + posChargeDatatmp.getId());
							// chargeSerivce.update(posChargeDatatmp);
							chargeSerivce.deleteById(posChargeDatatmp.getId());
						}

						jedisClient.del("P-" + parkId + "-" + charge.getCardNumber());
						jedisClient.del("query" + charge.getCardNumber());
						logger.info("重新入场 " + cardNumber + " id" + charge.getId());

					}

					jedisClient.set("carIn" + parkId + charge.getCardNumber(), String.valueOf(charge.getId()), 120);
					logger.info(cardNumber + "设置redis值" + charge.getId());
					jedisClient.set("carInTmp" + parkId + charge.getCardNumber(), "tmp", 20);
					jedisClient.set("query" + charge.getCardNumber(), String.valueOf(charge.getId()), 259200);
					jedisClient.set("P-" + parkId + "-" + charge.getCardNumber(), String.valueOf(charge.getId()),
							Constants.carMaxPeriod);

				}
			} catch (Exception e) {
				// TODO: handle exception
				logger.info(cardNumber + "redis异常" + e);
				dataMap.put("aT", "1");
				dataMap.put("cT", "in");
				dataMap.put("uT", "0");
				return Utility.createJsonMsg(1001, dataMap);
				// num = chargeSerivce.insert(charge);
			}

			if (num == 1) {

				// 发送topic队列
				if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
						|| park.getDescription().contains("本地计费")) {
					charge.setEntrance(true);
					ActiveMqService.SendTopicWithMac(charge, String.valueOf(parkId), mac, park.getPortLeftCount(),
							monthUserType);
				}

				try {

					if (!parktoaliparks.isEmpty()) {
						Parktoalipark parktoalipark = parktoaliparks.get(0);
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", parktoalipark.getAliparkingid());
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");

					}
					if (parknoticeauthority != null && parknoticeauthority.getAlipay() == true) {
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", "PI1501317472942184881");
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");
					}
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e);
					dataMap.put("aT", "1");
					dataMap.put("cT", "in");
					dataMap.put("uT", "0");
					return Utility.createJsonMsg(1001, dataMap);
				}
				logger.info(cardNumber + "入场成功!--" + dataMap.toString());
				ret.put("status", 1001);
			} else {
				ret.put("status", 1002);
			}
			return Utility.createJsonMsgWithoutMsg(1001, dataMap);
		} else {// 出场硬件
			logger.info(cardNumber + "--->开始出场");
			dataMap.put("cT", "out");
			dataMap.put("pT", "9");
			List<PosChargeData> queryCharges = null;
			String exitDate = (String) args.get("exitDate");

			if (jedisClient.get("carInTmp" + parkId + cardNumber) != null) {
				dataMap.put("aT", "2");
				logger.info("一分钟内 不许出场");
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			}
			logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
			int portLeftCount = park.getPortLeftCount() + 1;
			parkService.updateLeftPortCount(parkId, portLeftCount);
			logger.info(cardNumber + "离开" + park.getName() + "后车位数" + portLeftCount);
			if (monthuserNow != null) {
				realMonthUsers.add(monthuserNow);
			}

			// 取得出口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 0) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			if (exitDate != null) {

				try {
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);

				} catch (Exception e) {
					// TODO Auto-generated catch block
					return Utility.createJsonMsg(1002, "获取费用出现异常");
				}
			} else {
				try {
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
					// if (queryCharges.isEmpty() && outParkingFuzzyMatching(parkId, cardNumber) !=
					// null) {
					//
					// queryCharges = chargeSerivce.getDebtWithData(
					// outParkingFuzzyMatching(parkId, cardNumber).getCardNumber(), parktoaliparks,
					// realMonthUsers, park, isMultiFeeCtriterion, monthUserType, false);
					//
					// }
				} catch (Exception e) {
					return Utility.createJsonMsg(1002, e);
				}
			}
			logger.info(cardNumber + "计费结束!");
			double shouldChargeMoney = 0;

			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {
				logger.info(cardNumber + "没有未缴费!");
				FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());
				if (isRealMonthUser && realMonthUsers.size() == 1) {
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 以下就是查询停车费状态的部分迁移
				List<PosChargeData> posChargeDataList = chargeSerivce.getLastRecordWithPark(cardNumber, 1, parkId);

				if (posChargeDataList.isEmpty()) {
					dataMap.put("my", "0.0");
					dataMap.put("eD", "13");
					logger.info(cardNumber + "在停车场没有找到任何记录,费用为0出场");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				logger.info(cardNumber + "未找到未缴费,查询此停车场最后一条记录,id" + posChargeDataList.get(0).getId());
				PosChargeData posChargeData = posChargeDataList.get(0);
				// 支付类型
				dataMap.put("pT", String.valueOf(posChargeData.getPayType()));
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
				}
				Date payDate = posChargeData.getExitDate();
				if (posChargeData.getPayType() == 0 || posChargeData.getPayType() == 1) {
					logger.info(cardNumber + "已经通过移动进行支付");
					List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
					if (alipayrecords.isEmpty()) {
						payDate = new Date();
					} else {
						payDate = alipayrecords.get(0).getDate();
						logger.info(cardNumber + "已查询到支付记录:" + alipayrecords.get(0).getId() + "时间"
								+ new SimpleDateFormat(Constants.DATEFORMAT).format(payDate));
					}

				}
				if (payDate == null) {
					payDate = new Date();
				}
				long diff = new Date().getTime() - payDate.getTime();
				// 防止逃费
				if (posChargeData.getPayType() == 9 && diff < 1000 * 60 * 60 * 12) {
					dataMap.put("my", String.valueOf(posChargeData.getPaidMoney()));
					if (posChargeData.getChargeMoney() > 0.1) {
						dataMap.put("aT", "0");
					} else {
						dataMap.put("aT", "1");
					}

					dataMap.put("eD", String
							.valueOf((new Date().getTime() - posChargeData.getEntranceDate().getTime()) / (1000 * 60)));
					// if (Integer.valueOf((String) (dataMap.get("eD"))) > 360) {
					// dataMap.put("eD", "13");
					// }
					logger.info(cardNumber + "防逃费:" + dataMap.toString());
					if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
							|| park.getDescription().contains("本地计费")) {
						posChargeData.setEntrance(false);
						ActiveMqService.SendTopicWithMac(posChargeData, String.valueOf(parkId), mac,
								park.getPortLeftCount(), monthUserType);
					}
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}

				if (diff < 1000 * 60 * 15) {
					logger.info(cardNumber + "在支付完15分钟内离场");
					dataMap.put("my", "0.0");
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					long diff1 = (posChargeData.getExitDate().getTime() - posChargeData.getEntranceDate().getTime());
					dataMap.put("eD", String.valueOf((diff1 / (1000 * 60)) > 15 ? 13 : (diff1 / (1000 * 60))));

					posChargeData.setPaidCompleted(true);

					posChargeData.setUnPaidMoney(0);
					// if (posChargeData.getExitDate() == null) {
					// //posChargeData.setExitDate1(new Date());
					// posChargeData.setExitDate1(payDate);
					// }
					chargeSerivce.update(posChargeData);
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 超过了15分钟 小于2天
				else if (diff < 1000 * 60 * 60 * 6 && posChargeData.getPayType() != 2 && posChargeData.getPayType() != 9
						&& posChargeData.getPayType() < 20) {
					logger.info(cardNumber + "支付完后超过15分钟离场,将重新生成计费记录");
					// 超过15分钟 原纪录离场时间要更新
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(payDate);
						chargeSerivce.update(posChargeData);
					}
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(park.getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);
					jedisClient.set("P-" + parkId + "-" + charge2.getCardNumber(), String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);

					logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
					parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
					logger.info(cardNumber + "离开" + park.getName() + "后车位数" + park.getPortLeftCount() + 1);

					jedisClient.del("P-" + parkId + "-" + cardNumber);
					jedisClient.del("query" + cardNumber);
					logger.info("支付完超15分钟重新入场 " + cardNumber + " id" + charge2.getId() + "入场时间"
							+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					jedisClient.set("P-" + parkId + "-" + cardNumber, String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);
					if (num != 1) {
						return Utility.createJsonMsgWithoutMsg(1003, dataMap);
					}
					// 重新查询未缴费

					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				}
				// 如果大于2天,费用归0
				else {
					logger.info(cardNumber + "离上次计费超过两天,没有找到入场记录 判断为异常,免费放行");
					dataMap.put("my", "0.0");
					// 无入场记录 停车时长大于2天 强行按照13分钟处理
					dataMap.put("eD", "13");

					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 发送到队列
				if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true) {
					Map<String, String> argstoali = new HashMap<>();
					try {
						argstoali.put("parkName", posChargeData.getParkDesc());
						argstoali.put("carNumber", posChargeData.getCardNumber());
						argstoali.put("enterTime",
								new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(posChargeData.getEntranceDate()));
						argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
								posChargeData.getExitDate() == null ? new Date() : posChargeData.getExitDate()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
					} catch (Exception e) {
						logger.info(cardNumber + "weixinoutinfo：" + e);
					}
				}

			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(cardNumber + "计费完毕后处理");// 这段代码什么意思要讨论下
			int num = 0;
			if (!queryCharges.isEmpty()) {
				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(mac);
					if (posChargeData.getParkId() == parkId.intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);

						if (posChargeData.getPayType() < 20) { // 确保储值卡用户的支付类型不被修改
							posChargeData.setPayType(9);
						}

						charge.setPosId(mac);
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}
						chargeSerivce.update(posChargeData);
					} else {
						posChargeData.setChargeMoney(0.0);
						posChargeData.setPaidCompleted(true);
						if (posChargeData.getPayType() < 20) { // 确保储值卡用户的支付类型不被修改
							posChargeData.setPayType(9);
						}
						chargeSerivce.update(posChargeData);
					}
				}

				shouldChargeMoney = payRet.getChargeMoney();
				num = chargeSerivce.update(payRet);
			} else {
				logger.info(cardNumber + "查询计费结果为空!!!");
				return Utility.createJsonMsg(1002, "计费结果为空");
			}

			switch (monthUserType) {
			case 0:
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					if (payRet.getChargeMoney() > 0) {
						dataMap.put("aT", "0");
					} else {
						dataMap.put("aT", "1");
					}

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (parkcarauthority.getTemporary() != true && shouldChargeMoney > 0) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority.getTemporary0() != true && shouldChargeMoney < 0.1) {
					dataMap.put("aT", "0");
				}

				if (parkcarauthority2 != null) {
					parkcarauthority2
							.setCount((parkcarauthority2.getCount() - 1) < 0 ? 0 : (parkcarauthority2.getCount() - 1));
					parkcarauthority2Service.updateByPrimaryKeySelective(parkcarauthority2);
				}

				break;
			default:
				break;
			}

			// chargeSerivce.update(charge);

			// 发送到贵州
			if (parkId == 334) {
				Map<String, String> guiyang = new HashMap<>();
				guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
				guiyang.put("type", "out");
				guiyang.put("plateNum", cardNumber);
				guiyang.put("leaveTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
			}
			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
					|| park.getDescription().contains("本地计费")) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(parkId), mac, park.getPortLeftCount(),
						monthUserType);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, parkId.intValue())
					|| park.getDescription().contains("工行停车场")) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}
			// 如果储值卡有钱 则放行
			if (payRet.getPayType() == 20 && !monthusers.isEmpty() && monthusers.get(0).getPayment() > 0) {
				logger.info(cardNumber + "储值卡 放行");
				dataMap.put("aT", "1");
			}

			if ((!monthusers.isEmpty()) && monthusers.get(0).getType() == 2) {
				dataMap.put("uT", "3");
				monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
				dataMap.put("yue", String.valueOf(monthusers.get(0).getPayment()));
			}

			if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true && !queryCharges.isEmpty()) {
				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getEntranceDate()));
					argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getExitDate()));
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(cardNumber + "发送微信出场通知错误：" + e);
				}
			}

			if (payRet.getEntranceDate() == null) {

				return Utility.createJsonMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());

			dataMap.put("eD", String.valueOf((diff2 / (60 * 1000))));
			if (!isRealMonthUser) {
				dataMap.put("my", String.valueOf(payRet.getPaidMoney()));
			}
			if (isMultiCarsOneCarport && payRet.getParkDesc().contains("包月转临停")) {
				if (payRet.getChargeMoney() > 0) {
					dataMap.put("my", String.valueOf(payRet.getPaidMoney()));
					dataMap.put("uT", "0");
					dataMap.put("aT", "0");
				}
			}

			dataMap.put("pT", String.valueOf(payRet.getPayType()));

			if (num == 1) {
				if (isRealMonthUser && isMultiCarsOneCarport) {
					for (Monthuser tmpMonthuser : monthusers) {
						if (tmpMonthuser.getType() == 0 && tmpMonthuser.getPlatecolor() != null
								&& tmpMonthuser.getPlatecolor().equals("多车包月入场")) {
							tmpMonthuser.setPlatecolor("出场完结");
							monthUserService.updateByPrimaryKey(tmpMonthuser);
						}
					}
				}

				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());
				// posChargeMacService.insertSelective(poschargemac);
				jedisClient.set("out" + mac, String.valueOf(payRet.getId()));
				logger.info(cardNumber + "出场成功!" + dataMap.toString());

				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			} else {
				return Utility.createJsonMsg(1001, "ok");
			}
		}
	}

	public List<PosChargeData> outParkingFuzzyMatching(int parkId, String cardNumber) throws ParseException {
		logger.info("模糊匹配查询出场参数：" + parkId + "\t" + cardNumber);
		List<PosChargeData> posChargeData = null;
		List<PosChargeData> data = new ArrayList<>();
		List<String> cardNumbers = new ArrayList<>();
		Map<String, PosChargeData> maps = new HashMap<>();
		char[] bytes = cardNumber.toCharArray();
		data = chargeSerivce.getAllByDayRangeV1(parkId, null, null);
		for (PosChargeData pos : data) {
			cardNumbers.add(pos.getCardNumber());
			maps.put(pos.getCardNumber(), pos);
		}
		for (String cn : cardNumbers) {
			char[] cnchar = cn.toCharArray();
			int count = 0;
			int length = (bytes.length < cnchar.length) ? bytes.length : cnchar.length;
			for (int i = 0; i < length; i++) {
				if (bytes[i] == cnchar[i]) {
					count = count + 1;
				}
			}
			if (count == bytes.length - 2) {
				if (maps.get(cn) != null) {
					posChargeData.add(maps.get(cn));
				}
				logger.info(posChargeData);
			}
		}
		logger.info(
				"模糊匹配出场车辆记录：" + posChargeData + "\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		return posChargeData;
	}

	// 传递图片
	@RequestMapping(value = "platformtouch", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public String platformtouch(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		cardNumber = cardNumber.trim();
		logger.info("touch车辆" + cardNumber);
		String base64img = args.get("base64img");

		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		PosChargeData charge = new PosChargeData();
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1003);
			return Utility.gson.toJson(dataMap);
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			dataMap.put("aT", "1");
			dataMap.put("cT", "in");
			dataMap.put("uT", "0");
			return Utility.createJsonMsg(1001, dataMap);
		}
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		// 相似车牌管理
		List<Similarcarnumber> similarcarnumbers = similarCarNumberService.selectBySimilarCarNumberAndPark(cardNumber,
				parkId);
		if (!similarcarnumbers.isEmpty()) {
			cardNumber = similarcarnumbers.get(0).getRealnumber();
			logger.info("更正车牌:" + cardNumber);
		}

		List<Monthuser> monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
		Park park = parkService.getParkById(parkId);
		List<Parkcarauthority> parkcarauthorities = parkCarAuthorityService.getByParkId(parkId);
		if (parkcarauthorities.isEmpty()) {
			return null;
		}

		dataMap.put("aT", "1");

		boolean isMonthUser = false;
		boolean isRealMonthUser = false;
		int monthUserType = 9;
		Monthuser monthuserNow = null;
		if (!monthusers.isEmpty()) {
			isMonthUser = true;
			for (Monthuser tmpMonthuser : monthusers) {
				logger.info(cardNumber + "月卡id:" + tmpMonthuser.getId());
				Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
				monthuserNow = tmpMonthuser;
				if (tmpMonthuser.getType() == 0) {// 月卡
					monthUserType = 0;
					if (diff > 0) {
						int leftDays = (int) (diff / (1000 * 60 * 60 * 24));
						dataMap.put("ds", String.valueOf(leftDays));
						isRealMonthUser = true;
						dataMap.put("eD", new SimpleDateFormat(Constants.DATEFORMAT).format(tmpMonthuser.getEndtime()));
						break;
					} else {
						dataMap.put("ds", "-1");
						dataMap.put("uT", "1");
						monthUserType = 8; // 月卡过期
					}

				} else {
					if (diff >= 0) {
						monthUserType = tmpMonthuser.getType();
						break;
					} else {
						isMonthUser = false;
					}
				}
			}

		}

		if (!isMonthUser) {
			dataMap.put("uT", "0");
		} else {
			// Monthuser monthuser=monthusers.get(0);
			dataMap.put("uT", "1");
			// 判断是否是预约车
			if (monthUserType == 1) {
				dataMap.put("uT", "2");
			}
		}
		List<Parktoalipark> parktoaliparks = parkToAliparkService.getByParkId(parkId);
		Boolean isMultiCarsOneCarport = false;
		if (park.getDescription() != null && park.getDescription().contains("一位多车")) {
			logger.info(cardNumber + "进入一位多车停车场");
			isMultiCarsOneCarport = true;
		}
		Boolean isMultiFeeCtriterion = false;
		if (park.getDescription() != null && park.getDescription().contains("多计费标准")) {
			isMultiFeeCtriterion = true;
		}
		// 判断是否有多个车
		List<Monthuser> realMonthUsers = new ArrayList<>();
		if (monthuserNow != null && monthuserNow.getCardnumber() != null
				&& !monthuserNow.getCardnumber().trim().equals("")) {
			List<Monthuser> monthuserss = monthUserService.getByParkAndPort(monthuserNow.getParkid(),
					monthuserNow.getCardnumber());
			for (Monthuser monthuser : monthuserss) {
				logger.info(cardNumber + "多个车" + monthuser.getId());
				if (monthuser.getType().intValue() == 0) {
					realMonthUsers.add(monthuser);
				}
			}
		}

		Parknoticeauthority parknoticeauthority = parkNoticeAuthorityService.getByParkId(parkId).get(0);
		Parkcarauthority2 parkcarauthority2 = parkcarauthority2Service.selectByPark(parkId);

		// 传递图片
		List<PosChargeData> posChargeDatasimg = chargeSerivce.getByCardNumberAndPark(cardNumber, parkId);
		if (!posChargeDatasimg.isEmpty() && StringUtils.isEmpty(base64img)) {
			String url = FileUploadService.imgBase64Upload(base64img);
			if (channelFlag == 1) {
				dataMap.put("status", 1001);
				dataMap.put("message", "入口成功");
				dataMap.put("body", url);
				posChargeDatasimg.get(0).setUrl(url);

			} else {
				dataMap.put("status", 1001);
				dataMap.put("message", "出口成功");
				dataMap.put("body", url);
				posChargeDatasimg.get(0).setOutUrl(url);
			}
		}

		// 入口硬件
		if (channelFlag == 1) {
			if (largeCar == true) {
				charge.setIsLargeCar(true);
			}

			charge.setParkDesc(park.getName() + "-临停车");
			// 取得入口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 1) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			// 贵州队列发送
			if (parkId == 334) {
				Map<String, String> guiyang = new HashMap<>();
				guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
				guiyang.put("type", "in");
				guiyang.put("plateNum", cardNumber);
				guiyang.put("enterTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
			}

			dataMap.put("cT", "in");
			charge.setCardNumber(cardNumber);
			charge.setParkId(parkId);

			// 判断包月车已入场

			Boolean isMonthUserCarIn = false;
			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				for (Monthuser tmMonthuser : realMonthUsers) {
					if (tmMonthuser.getPlatecolor() != null && tmMonthuser.getPlatecolor().equals("多车包月入场")) {
						logger.info(cardNumber + "同车位已经入场Id" + tmMonthuser.getId());
						if (!tmMonthuser.getPlatenumber().equals(cardNumber)) {
							isMonthUserCarIn = true;
						}

					}
				}
				monthuserNow.setPlatecolor("多车包月入场");
				if (isMonthUserCarIn) {
					charge.setParkDesc(park.getName() + "-包月转临停");
					monthuserNow.setPlatecolor("包月转临停");
					dataMap.put("uT", "0");
					monthUserType = 9;
					try {
						dataMap.remove("ds");
						dataMap.remove("eD");
					} catch (Exception e) {
						// TODO: handle exception
					}

				}
				monthUserService.updateByPrimaryKeySelective(monthuserNow);
			}

			switch (monthUserType) {
			case 0:
				charge.setParkDesc(park.getName() + "-包月车");
				charge.setRejectReason(monthusers.get(0).getOwner());
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (!charge.getParkDesc().contains("包月转临停")) {
					charge.setParkDesc(park.getName() + "-临停车");
				}

				if (parkcarauthority.getTemporary() != true) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority2 != null) {
					logger.info("临停数量" + parkcarauthority2.getCount());
					if (parkcarauthority2.getCount() >= parkcarauthority2.getMaxcount()) {
						dataMap.put("aT", "0");
						logger.info(park.getId() + "-" + cardNumber + "-临停数量达到限制");
					} else {
						parkcarauthority2.setCount(parkcarauthority2.getCount() + 1);
						parkcarauthority2Service.updateByPrimaryKey(parkcarauthority2);
					}
				}
				break;
			default:
				break;
			}

			if (!monthusers.isEmpty()) {
				charge.setRejectReason(monthusers.get(0).getOwner());
			}
			charge.setPosId(mac);
			charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			int num = 0;
			// redis查询
			try {
				String redisStr = jedisClient.get("carIn" + parkId + charge.getCardNumber());
				if (redisStr != null) {
					num = 1;
					logger.info("redis检测重复入场" + cardNumber + redisStr);
				} else {
					num = chargeSerivce.insert(charge);
					// 清场
					String redisId = jedisClient.get("P-" + park.getId() + "-" + cardNumber);
					if (redisId != null) {
						PosChargeData posChargeDatatmp = chargeDao.getById(Integer.parseInt(redisId));
						if (posChargeDatatmp != null && !posChargeDatatmp.isPaidCompleted()) {
							posChargeDatatmp.setChargeMoney(0);
							posChargeDatatmp.setExitDate1(posChargeDatatmp.getEntranceDate());
							posChargeDatatmp.setPaidCompleted(true);
							posChargeDatatmp.setOperatorId("redis");
							posChargeDatatmp.setRejectReason("平台清场r");
							logger.info(cardNumber + "清场" + posChargeDatatmp.getId());
							// chargeSerivce.update(posChargeDatatmp);
							chargeSerivce.deleteById(posChargeDatatmp.getId());
						}

						jedisClient.del("P-" + parkId + "-" + charge.getCardNumber());
						logger.info("重新入场 " + cardNumber + " id" + charge.getId());

					}

					jedisClient.set("carIn" + parkId + charge.getCardNumber(), String.valueOf(charge.getId()), 120);
					jedisClient.set("carInTmp" + parkId + charge.getCardNumber(), "tmp", 20);
					jedisClient.set("P-" + parkId + "-" + charge.getCardNumber(), String.valueOf(charge.getId()),
							Constants.carMaxPeriod);

				}
			} catch (Exception e) {
				// TODO: handle exception
				num = chargeSerivce.insert(charge);
			}

			if (num == 1) {

				// 发送topic队列
				if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
						|| park.getDescription().contains("本地计费")) {
					charge.setEntrance(true);
					ActiveMqService.SendTopicWithMac(charge, String.valueOf(parkId), mac, park.getPortLeftCount(),
							monthUserType);
				}

				try {

					if (!parktoaliparks.isEmpty()) {
						Parktoalipark parktoalipark = parktoaliparks.get(0);
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", parktoalipark.getAliparkingid());
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");

					}
					if (parknoticeauthority != null && parknoticeauthority.getAlipay() == true) {
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", "PI1501317472942184881");
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");
					}
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e);
				}
				logger.info(cardNumber + "入场成功!" + dataMap.toString());
				ret.put("status", 1001);
			} else {
				ret.put("status", 1002);
			}
			return Utility.createJsonMsgWithoutMsg(1001, dataMap);
		} else {// 出场硬件
			dataMap.put("cT", "out");
			dataMap.put("pT", "9");

			List<PosChargeData> queryCharges = null;
			String exitDate = (String) args.get("exitDate");
			logger.info(cardNumber + "开始出场");

			if (jedisClient.get("carInTmp" + parkId + cardNumber) != null) {
				dataMap.put("aT", "2");
				logger.info("一分钟内 不许出场");
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			}

			logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
			parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
			logger.info(cardNumber + "离开" + park.getName() + "后车位数" + park.getPortLeftCount() + 1);

			if (monthuserNow != null) {
				realMonthUsers.add(monthuserNow);
			}

			// 取得出口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 0) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			if (exitDate != null) {

				try {

					// queryCharges = chargeSerivce.getDebt(cardNumber, eDate);
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);

				} catch (Exception e) {
					// TODO Auto-generated catch block
					return Utility.createJsonMsg(1002, "获取费用出现异常");
				}
			} else {
				try {

					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);

				} catch (Exception e) {
					return Utility.createJsonMsg(1002, e);
				}
			}
			logger.info(cardNumber + "计费结束!");
			double shouldChargeMoney = 0;
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {
				logger.info(cardNumber + "没有未缴费!");
				FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());

				if (isRealMonthUser && realMonthUsers.size() == 1) {
					// dataMap.put("my", "0.0");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 以下就是查询停车费状态的部分迁移
				List<PosChargeData> posChargeDataList = chargeSerivce.getLastRecordWithPark(cardNumber, 1, parkId);
				if (posChargeDataList.isEmpty()) {
					dataMap.put("my", "0.0");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				PosChargeData posChargeData = posChargeDataList.get(0);
				// 支付类型
				dataMap.put("pT", String.valueOf(posChargeData.getPayType()));
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
				}

				// 发送到队列

				if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true) {
					Map<String, String> argstoali = new HashMap<>();
					try {
						argstoali.put("parkName", posChargeData.getParkDesc());
						argstoali.put("carNumber", posChargeData.getCardNumber());
						argstoali.put("enterTime",
								new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(posChargeData.getEntranceDate()));
						argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
								posChargeData.getExitDate() == null ? new Date() : posChargeData.getExitDate()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
					} catch (Exception e) {
						// TODO: handle exception
						logger.info(cardNumber + "weixinoutinfo：" + e);
					}

				}

				Date payDate = new Date();
				if (posChargeData.getPayType() == 0 || posChargeData.getPayType() == 1) {
					List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
					if (alipayrecords.isEmpty()) {
						// dataMap.put("my", "0.0");
						payDate = new Date();
					}
					payDate = alipayrecords.get(0).getDate();
				}
				if (payDate == null) {
					payDate = new Date();
				}

				long diff = new Date().getTime() - payDate.getTime();
				if (diff < 1000 * 60 * 15) {
					dataMap.put("my", "0.0");
					long diff1 = (posChargeData.getExitDate().getTime() - posChargeData.getEntranceDate().getTime());
					dataMap.put("eD", String.valueOf(diff1 / (1000 * 60)));
					posChargeData.setPaidCompleted(true);
					// posChargeData.setPayType(9);
					posChargeData.setPaidMoney(posChargeData.getChargeMoney());
					posChargeData.setUnPaidMoney(0);
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					// posChargeData.setOperatorId("15minPaid");
					chargeSerivce.update(posChargeData);

					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 超过了15分钟
				else if (posChargeData.getPayType() != 2 && posChargeData.getPayType() != 9) {

				}
				{

					// 超过15分钟 原纪录离场时间要更新
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(payDate);
						chargeSerivce.update(posChargeData);
					}

					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(park.getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);

					logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
					parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
					logger.info(cardNumber + "离开" + park.getName() + "后车位数" + park.getPortLeftCount() + 1);

					jedisClient.del("P-" + parkId + "-" + cardNumber);
					jedisClient.del("query" + cardNumber);
					logger.info("支付完超15分钟重新入场 " + cardNumber + " id" + charge2.getId());
					jedisClient.set("P-" + parkId + "-" + cardNumber, String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);
					if (num != 1) {
						return Utility.createJsonMsgWithoutMsg(1003, dataMap);
					}
					// 重新查询未缴费
					// queryCharges = chargeSerivce.getDebt(cardNumber);
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				}
			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(cardNumber + "计费完毕后处理");
			int num = 0;
			if (!queryCharges.isEmpty()) {
				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(mac);
					if (posChargeData.getParkId() == parkId.intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);
						if (posChargeData.getPayType() < 20) { // 确保储值卡用户的支付类型不被修改
							posChargeData.setPayType(9);
						}
						charge.setPosId(mac);
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}
						chargeSerivce.update(posChargeData);
					} else {
						posChargeData.setChargeMoney(0.0);
						posChargeData.setPaidCompleted(true);
						if (posChargeData.getPayType() < 20) { // 确保储值卡用户的支付类型不被修改
							posChargeData.setPayType(9);
						} else {
						}
						chargeSerivce.update(posChargeData);
					}
				}

				shouldChargeMoney = payRet.getChargeMoney();
				num = chargeSerivce.update(payRet);
			} else {
				logger.info(cardNumber + "查询计费结果为空!!!");
				return Utility.createJsonMsg(1002, "计费结果为空");
			}

			switch (monthUserType) {
			case 0:
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					if (payRet.getChargeMoney() > 0) {
						dataMap.put("aT", "0");
					}

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (parkcarauthority.getTemporary() != true && shouldChargeMoney > 0) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority.getTemporary0() != true && shouldChargeMoney < 0.1) {
					dataMap.put("aT", "0");
				}

				if (parkcarauthority2 != null) {
					parkcarauthority2
							.setCount((parkcarauthority2.getCount() - 1) < 0 ? 0 : (parkcarauthority2.getCount() - 1));
					parkcarauthority2Service.updateByPrimaryKeySelective(parkcarauthority2);
				}

				break;
			default:
				break;
			}
			// 发送到贵州
			if (parkId == 334) {
				Map<String, String> guiyang = new HashMap<>();
				guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
				guiyang.put("type", "out");
				guiyang.put("plateNum", cardNumber);
				guiyang.put("leaveTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
			}
			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
					|| park.getDescription().contains("本地计费")) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(parkId), mac, park.getPortLeftCount(),
						monthUserType);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, parkId.intValue())
					|| park.getDescription().contains("工行停车场")) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}

			if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true && !queryCharges.isEmpty()) {
				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getEntranceDate()));
					argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getExitDate()));
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(cardNumber + "发送微信出场通知错误：" + e);
				}
			}

			if (payRet.getEntranceDate() == null) {

				return Utility.createJsonMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());
			dataMap.put("eD", String.valueOf((diff2 / (60 * 1000)) > 636000 ? 13 : (diff2 / (60 * 1000))));
			if (!isRealMonthUser) {
				dataMap.put("my", String.valueOf(payRet.getPaidMoney()));
			}
			if (isMultiCarsOneCarport && payRet.getParkDesc().contains("包月转临停")) {
				if (payRet.getChargeMoney() > 0) {
					dataMap.put("my", String.valueOf(payRet.getPaidMoney()));
					dataMap.put("uT", "0");
					dataMap.put("aT", "0");
				}
			}

			dataMap.put("pT", String.valueOf(payRet.getPayType()));

			if (num == 1) {
				// Poschargemac poschargemac = new Poschargemac();
				// poschargemac.setMacidout((int) info.get("macId"));
				// poschargemac.setPoschargeid(payRet.getId());
				// posChargeMacService.updateByPosChargeId(poschargemac);
				if (isRealMonthUser && isMultiCarsOneCarport) {
					for (Monthuser tmpMonthuser : monthusers) {
						if (tmpMonthuser.getType() == 0 && tmpMonthuser.getPlatecolor() != null
								&& tmpMonthuser.getPlatecolor().equals("多车包月入场")) {
							tmpMonthuser.setPlatecolor("出场完结");
							monthUserService.updateByPrimaryKey(tmpMonthuser);
						}
					}
				}

				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());
				// posChargeMacService.insertSelective(poschargemac);
				jedisClient.set("out" + mac, String.valueOf(payRet.getId()));
				logger.info(cardNumber + "出场成功!" + dataMap.toString());
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			} else {
				return Utility.createJsonMsg(1001, "ok");
			}
		}
	}

	// left 剩余车位为0 不允许进场
	@RequestMapping(value = "touchedLeftCPark", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public String touchedLeftCPark(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		cardNumber = cardNumber.trim();
		logger.info("touch车辆" + cardNumber);
		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		PosChargeData charge = new PosChargeData();
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getHardwareByMacPark(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1002);
			return Utility.gson.toJson(dataMap);
		}
		Map<String, Object> info = infos.get(0);
		int parkId = (int) info.get("parkID");
		if (info == null) {
			return Utility.createJsonMsgWithoutMsg(1002, dataMap);
		}
		Park park = parkService.getParkById(parkId);
		dataMap.put("lT", String.valueOf(park.getPortLeftCount()));
		dataMap.put("aT", "1");
		dataMap.put("cT", "out");
		dataMap.put("uT", "0");
		dataMap.put("my", "0.0");
		dataMap.put("pT", "9");
		return Utility.createJsonMsgWithoutMsg(1001, dataMap);
	}

	// left 剩余车位为0 不允许进场
	@RequestMapping(value = "touchedLeft", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String touchedLeft(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		cardNumber = cardNumber.trim();
		logger.info("touch车辆" + cardNumber);

		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		PosChargeData charge = new PosChargeData();
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1003);
			return Utility.gson.toJson(dataMap);
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			dataMap.put("aT", "1");
			dataMap.put("cT", "in");
			dataMap.put("uT", "0");
			return Utility.createJsonMsg(1001, dataMap);
		}
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		// 相似车牌管理
		List<Similarcarnumber> similarcarnumbers = similarCarNumberService.selectBySimilarCarNumberAndPark(cardNumber,
				parkId);
		if (!similarcarnumbers.isEmpty()) {
			cardNumber = similarcarnumbers.get(0).getRealnumber();
			logger.info("更正车牌:" + cardNumber);
		}

		List<Monthuser> monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
		Park park = parkService.getParkById(parkId);
		List<Parkcarauthority> parkcarauthorities = parkCarAuthorityService.getByParkId(parkId);
		if (parkcarauthorities.isEmpty()) {
			return null;
		}

		dataMap.put("aT", "1");

		boolean isMonthUser = false;
		boolean isRealMonthUser = false;
		int monthUserType = 9;
		Monthuser monthuserNow = null;
		if (!monthusers.isEmpty()) {
			isMonthUser = true;
			for (Monthuser tmpMonthuser : monthusers) {
				Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
				monthuserNow = tmpMonthuser;
				if (tmpMonthuser.getType() == 0) {// 月卡
					monthUserType = 0;
					if (diff > 0) {
						int leftDays = (int) (diff / (1000 * 60 * 60 * 24));
						dataMap.put("ds", String.valueOf(leftDays));
						isRealMonthUser = true;
						dataMap.put("eD", new SimpleDateFormat(Constants.DATEFORMAT).format(tmpMonthuser.getEndtime()));
						break;
					} else {
						dataMap.put("ds", "-1");
						dataMap.put("uT", "1");
						monthUserType = 8; // 月卡过期
					}

				} else {
					if (diff >= 0) {
						monthUserType = tmpMonthuser.getType();
						break;
					} else {
						isMonthUser = false;
					}
				}
			}

		}

		if (!isMonthUser) {
			dataMap.put("uT", "0");
		} else {
			// Monthuser monthuser=monthusers.get(0);
			dataMap.put("uT", "1");
			// 判断是否是预约车
			if (monthUserType == 1) {
				dataMap.put("uT", "2");
			}
		}
		List<Parktoalipark> parktoaliparks = parkToAliparkService.getByParkId(parkId);
		Boolean isMultiCarsOneCarport = false;
		if (park.getDescription() != null && park.getDescription().contains("一位多车")) {
			logger.info(cardNumber + "进入一位多车停车场");
			isMultiCarsOneCarport = true;
		}
		Boolean isMultiFeeCtriterion = false;
		if (park.getDescription() != null && park.getDescription().contains("多计费标准")) {
			isMultiFeeCtriterion = true;
		}
		// 判断是否有多个车
		List<Monthuser> realMonthUsers = new ArrayList<>();
		if (monthuserNow != null && monthuserNow.getCardnumber() != null && !monthuserNow.getCardnumber().equals("")) {
			List<Monthuser> monthuserss = monthUserService.getByParkAndPort(monthuserNow.getParkid(),
					monthuserNow.getCardnumber());
			for (Monthuser monthuser : monthuserss) {
				if (monthuser.getType().intValue() == 0) {
					realMonthUsers.add(monthuser);
				}
			}
		}

		Parknoticeauthority parknoticeauthority = parkNoticeAuthorityService.getByParkId(parkId).get(0);

		dataMap.put("lT", String.valueOf(park.getPortLeftCount()));

		// 入口硬件
		if (channelFlag == 1) {
			if (largeCar == true) {
				charge.setIsLargeCar(true);
			}

			charge.setParkDesc(park.getName() + "-临停车");
			// 取得入口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 1) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			// 贵州队列发送
			if (parkId == 334) {
				Map<String, String> guiyang = new HashMap<>();
				guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
				guiyang.put("type", "in");
				guiyang.put("plateNum", cardNumber);
				guiyang.put("enterTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
			}

			switch (monthUserType) {
			case 0:
				charge.setParkDesc(park.getName() + "-包月车");
				charge.setRejectReason(monthusers.get(0).getOwner());
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				charge.setParkDesc(park.getName() + "-临停车");
				if (parkcarauthority.getTemporary() != true) {
					dataMap.put("aT", "0");

				}
				break;
			default:
				break;
			}

			dataMap.put("cT", "in");
			charge.setCardNumber(cardNumber);
			charge.setParkId(parkId);

			// 判断包月车已入场

			Boolean isMonthUserCarIn = false;
			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				for (Monthuser tmMonthuser : realMonthUsers) {
					if (tmMonthuser.getPlatecolor() != null && tmMonthuser.getPlatecolor().equals("多车包月入场")) {
						if (!tmMonthuser.getPlatenumber().equals(cardNumber)) {
							isMonthUserCarIn = true;
						}

					}
				}
				monthuserNow.setPlatecolor("多车包月入场");
				if (isMonthUserCarIn) {
					charge.setParkDesc(park.getName() + "-包月转临停");
					monthuserNow.setPlatecolor("包月转为临停");
					dataMap.put("uT", "0");
					try {
						dataMap.remove("ds");
						dataMap.remove("eD");
					} catch (Exception e) {
						// TODO: handle exception
					}

				}
				monthUserService.updateByPrimaryKeySelective(monthuserNow);
			}
			charge.setPosId(mac);
			charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

			if (park.getPortLeftCount() == 0 && monthUserType != 0) {
				dataMap.put("aT", "0");
				dataMap.put("lT", "0");
				logger.info("无车位拒绝入场" + cardNumber);
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			}

			int num = 0;
			// redis查询
			try {
				String redisStr = jedisClient.get("carIn" + parkId + charge.getCardNumber());
				if (redisStr != null) {
					num = 1;
					logger.info("redis检测重复入场" + cardNumber + redisStr);
				} else {
					num = chargeSerivce.insert(charge);
					// 清场
					String redisId = jedisClient.get("P-" + park.getId() + "-" + cardNumber);
					if (redisId != null) {
						PosChargeData posChargeDatatmp = chargeDao.getById(Integer.parseInt(redisId));
						if (posChargeDatatmp != null && !posChargeDatatmp.isPaidCompleted()) {
							posChargeDatatmp.setChargeMoney(0);
							posChargeDatatmp.setExitDate1(posChargeDatatmp.getEntranceDate());
							posChargeDatatmp.setPaidCompleted(true);
							posChargeDatatmp.setOperatorId("redis");
							posChargeDatatmp.setRejectReason("平台清场r");
							logger.info(cardNumber + "清场" + posChargeDatatmp.getId());
							// chargeSerivce.update(posChargeDatatmp);
							chargeSerivce.deleteById(posChargeDatatmp.getId());
						}

						jedisClient.del("P-" + parkId + "-" + charge.getCardNumber());
						jedisClient.del("query" + cardNumber);
						logger.info("重新入场 " + cardNumber + " id" + charge.getId());

					}

					jedisClient.set("carIn" + parkId + charge.getCardNumber(), String.valueOf(charge.getId()), 120);
					jedisClient.set("query" + cardNumber, String.valueOf(charge.getId()), 259200);
					jedisClient.set("P-" + parkId + "-" + charge.getCardNumber(), String.valueOf(charge.getId()),
							Constants.carMaxPeriod);
				}
			} catch (Exception e) {
				// TODO: handle exception
				num = chargeSerivce.insert(charge);
			}

			if (num == 1) {

				// 发送topic队列
				if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())) {
					charge.setEntrance(true);
					ActiveMqService.SendTopicWithMac(charge, String.valueOf(parkId), mac, park.getPortLeftCount(),
							monthUserType);
				}
				// Poschargemac poschargemac = new Poschargemac();
				// poschargemac.setMacidenter((int) info.get("macId"));
				// poschargemac.setPoschargeid(charge.getId());
				// posChargeMacService.insertSelective(poschargemac);
				try {

					if (!parktoaliparks.isEmpty()) {
						Parktoalipark parktoalipark = parktoaliparks.get(0);
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", parktoalipark.getAliparkingid());
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");

					} else {
						// Map<String, String> argstoali = new HashMap<>();
						// argstoali.put("parking_id", "PI1501317472942184881");
						// argstoali.put("car_number", cardNumber);
						// argstoali.put("in_time", new
						// SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new
						// Date()));
						// aliparkFeeService.parkingEnterinfoSync(argstoali);
					}

					if (parknoticeauthority != null && parknoticeauthority.getAlipay() == true) {
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", "PI1501317472942184881");
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");
					}
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e);
					dataMap.put("aT", "1");
					dataMap.put("cT", "in");
					dataMap.put("uT", "0");
					return Utility.createJsonMsg(1001, dataMap);
				}
				logger.info(cardNumber + "入场成功!" + dataMap.toString());
				ret.put("status", 1001);
			} else {
				ret.put("status", 1002);
			}
			return Utility.createJsonMsgWithoutMsg(1001, dataMap);
		} else { // 出场硬件
			dataMap.put("cT", "out");
			dataMap.put("pT", "9");
			List<PosChargeData> queryCharges = null;
			String exitDate = (String) args.get("exitDate");
			logger.info(cardNumber + "开始出场");
			logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
			parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
			logger.info(cardNumber + "离开" + park.getName() + "后车位数" + park.getPortLeftCount() + 1);
			if (monthuserNow != null) {
				realMonthUsers.add(monthuserNow);
			}

			// 取得出口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 0) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			if (exitDate != null) {
				// Date eDate = new
				// SimpleDateFormat(Constants.DATEFORMAT).parse(exitDate);
				try {
					// System.out.println("出场时间为空,将要进行getDebt计算: "+new
					// Date().getTime()+"\n");
					// queryCharges = chargeSerivce.getDebt(cardNumber, eDate);
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);

					// System.out.println("出场时间为空,getDebt计算完毕: "+new
					// Date().getTime()+"\n");

				} catch (Exception e) {
					// TODO Auto-generated catch block
					return Utility.createJsonMsg(1002, "获取费用出现异常");
				}
			} else {
				try {
					// System.out.println("出场时间不为空,将要进行getDebt计算: "+new
					// Date().getTime()+"\n");
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
					// System.out.println("出场时间不为空,getDebt计算完毕: "+new
					// Date().getTime()+"\n");
				} catch (Exception e) {
					return Utility.createJsonMsg(1002, e);
				}
			}
			logger.info(cardNumber + "计费结束!");
			double shouldChargeMoney = 0;
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {
				logger.info(cardNumber + "没有未缴费!");
				FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());
				// if (largeCar==true) {
				// charge.setIsLargeCar(true);
				// }
				// charge.setCardNumber(cardNumber);
				// charge.setParkId(parkId);
				// charge.setParkDesc(parkName);
				// charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd
				// HH:mm:ss").format(new Date()));
				// chargeSerivce.insert(charge);
				/*
				 * if (!parktoaliparks.isEmpty()) { Parktoalipark
				 * parktoalipark=parktoaliparks.get(0); Map<String, String> argstoali=new
				 * HashMap<>(); argstoali.put("parking_id", parktoalipark.getAliparkingid());
				 * argstoali.put("car_number", cardNumber); argstoali.put("out_time", new
				 * SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format(new Date()));
				 * System.out.println("支付宝同步出场计算前: "+new Date().getTime()+"\n");
				 * aliparkFeeService.parkingExitinfoSync(argstoali);
				 * System.out.println("支付宝同步出场计算后: "+new Date().getTime()+"\n"); }
				 */
				if (isRealMonthUser && realMonthUsers.size() == 1) {
					// dataMap.put("my", "0.0");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 以下就是查询停车费状态的部分迁移
				List<PosChargeData> posChargeDataList = chargeSerivce.getLastRecordWithPark(cardNumber, 1, parkId);
				if (posChargeDataList.isEmpty()) {
					dataMap.put("my", "0.0");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				PosChargeData posChargeData = posChargeDataList.get(0);
				// 支付类型
				dataMap.put("pT", String.valueOf(posChargeData.getPayType()));
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
				}

				// 发送到队列

				if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true) {
					Map<String, String> argstoali = new HashMap<>();
					try {
						argstoali.put("parkName", posChargeData.getParkDesc());
						argstoali.put("carNumber", posChargeData.getCardNumber());
						argstoali.put("enterTime",
								new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(posChargeData.getEntranceDate()));
						argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
								posChargeData.getExitDate() == null ? new Date() : posChargeData.getExitDate()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
					} catch (Exception e) {
						// TODO: handle exception
						logger.info(cardNumber + "weixinoutinfo：" + e);
					}

				}

				Date payDate = new Date();
				if (posChargeData.getPayType() == 0 || posChargeData.getPayType() == 1) {
					List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
					if (alipayrecords.isEmpty()) {
						// dataMap.put("my", "0.0");
						payDate = new Date();
					}
					payDate = alipayrecords.get(0).getDate();
				}
				if (payDate == null) {
					payDate = new Date();
				}

				long diff = new Date().getTime() - payDate.getTime();
				if (diff < 1000 * 60 * 15) {
					logger.info(cardNumber + "支付完15分钟内出场");
					dataMap.put("my", "0.0");
					long diff1 = (posChargeData.getExitDate().getTime() - posChargeData.getEntranceDate().getTime());
					dataMap.put("eD", String.valueOf((diff1 / (1000 * 60)) > 15 ? 13 : (diff1 / (1000 * 60))));
					posChargeData.setPaidCompleted(true);
					// posChargeData.setPayType(9);
					posChargeData.setPaidMoney(posChargeData.getChargeMoney());
					posChargeData.setUnPaidMoney(0);
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					// posChargeData.setOperatorId("15minPaid");
					chargeSerivce.update(posChargeData);

					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 超过了15分钟
				else {
					logger.info(cardNumber + "超时15分钟 重新入场");

					// 超过15分钟 原纪录离场时间要更新
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(payDate);
						chargeSerivce.update(posChargeData);
					}
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(park.getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);
					parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);// 补偿加1
					jedisClient.del("P-" + parkId + "-" + cardNumber);
					jedisClient.del("query" + cardNumber);
					logger.info("支付完超15分钟重新入场 " + cardNumber + " id" + charge2.getId());
					jedisClient.set("P-" + parkId + "-" + cardNumber, String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);
					if (num != 1) {
						return Utility.createJsonMsgWithoutMsg(1003, dataMap);
					}
					// 重新查询未缴费
					// queryCharges = chargeSerivce.getDebt(cardNumber);
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				}
			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(cardNumber + "计费完毕后处理");
			int num = 0;
			if (!queryCharges.isEmpty()) {
				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(mac);
					if (posChargeData.getParkId() == parkId.intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);
						posChargeData.setPayType(9);
						// posChargeData.setOperatorId("道闸");
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}
						chargeSerivce.update(posChargeData);
					} else {
						posChargeData.setChargeMoney(0.0);
						posChargeData.setPaidCompleted(true);
						posChargeData.setPayType(9);
						chargeSerivce.update(posChargeData);
					}
				}

				shouldChargeMoney = payRet.getChargeMoney();
				num = chargeSerivce.update(payRet);
			} else {
				logger.info(cardNumber + "查询计费结果为空!!!");
				return Utility.createJsonMsg(1002, "计费结果为空");
			}

			switch (monthUserType) {
			case 0:
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (parkcarauthority.getTemporary() != true && shouldChargeMoney > 0) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority.getTemporary0() != true && shouldChargeMoney < 0.1) {
					dataMap.put("aT", "0");
				}
				break;
			default:
				break;
			}

			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(parkId), mac, park.getPortLeftCount(),
						monthUserType);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, parkId.intValue())
					|| park.getDescription().contains("工行停车场")) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}

			if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true && !queryCharges.isEmpty()) {
				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getEntranceDate()));
					argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getExitDate()));
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(cardNumber + "发送微信出场通知错误：" + e);
				}
			}

			if (payRet.getEntranceDate() == null) {

				return Utility.createJsonMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());
			dataMap.put("eD", String.valueOf((diff2 / (60 * 1000)) > 1360 ? 13 : (diff2 / (60 * 1000))));
			if (!isRealMonthUser) {
				dataMap.put("my", String.valueOf(payRet.getPaidMoney()));
			}
			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				if (payRet.getChargeMoney() > 0) {
					dataMap.put("my", String.valueOf(payRet.getPaidMoney()));
					dataMap.put("uT", "0");
				}
			}

			dataMap.put("pT", String.valueOf(payRet.getPayType()));

			if (num == 1) {

				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());
				// posChargeMacService.insertSelective(poschargemac);
				jedisClient.set("out" + mac, String.valueOf(payRet.getId()));
				logger.info(cardNumber + "出场成功!" + dataMap.toString());

				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			} else {
				return Utility.createJsonMsg(1001, "ok");
			}
		}
	}

	// 合肥touched 主要是针对到道闸又返回 再出费用为0做的修补
	@RequestMapping(value = "hefeitouched", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public String hefeitouched(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		logger.info(cardNumber + "--->进入[hefeitouched]车辆入场/出场接口，mac " + mac);

		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		PosChargeData charge = new PosChargeData();
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1003);
			return Utility.gson.toJson(dataMap);
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			dataMap.put("aT", "1");
			dataMap.put("cT", "in");
			dataMap.put("uT", "0");
			return Utility.createJsonMsg(1001, dataMap);
		}
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		// 相似车牌管理
		List<Similarcarnumber> similarcarnumbers = similarCarNumberService.selectBySimilarCarNumberAndPark(cardNumber,
				parkId);
		if (!similarcarnumbers.isEmpty()) {
			cardNumber = similarcarnumbers.get(0).getRealnumber();
			logger.info("更正车牌:" + cardNumber);
		}

		List<Monthuser> monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
		Park park = parkService.getParkById(parkId);
		List<Parkcarauthority> parkcarauthorities = parkCarAuthorityService.getByParkId(parkId);
		if (parkcarauthorities.isEmpty()) {
			return null;
		}

		dataMap.put("aT", "1");
		boolean isMonthUser = false;
		boolean isRealMonthUser = false;
		int monthUserType = 9;
		Monthuser monthuserNow = null;
		if (!monthusers.isEmpty()) {
			isMonthUser = true;
			for (Monthuser tmpMonthuser : monthusers) {
				logger.info(cardNumber + "月卡id:" + tmpMonthuser.getId() + "类型:" + tmpMonthuser.getType() + "到期时间:"
						+ Utility.formatDatetime(tmpMonthuser.getEndtime()));
				Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
				monthuserNow = tmpMonthuser;
				if (tmpMonthuser.getType() == 0) {// 月卡
					monthUserType = 0;
					if (diff > 0) {
						int leftDays = (int) (diff / (1000 * 60 * 60 * 24));
						dataMap.put("ds", String.valueOf(leftDays));
						isRealMonthUser = true;
						dataMap.put("my", "0.0");
						dataMap.put("eD", new SimpleDateFormat(Constants.DATEFORMAT).format(tmpMonthuser.getEndtime()));
						break;
					} else {
						dataMap.put("ds", "-1");
						dataMap.put("uT", "1");
						monthUserType = 8; // 月卡过期
					}

				} else {
					if (diff >= 0) {
						monthUserType = tmpMonthuser.getType();
						break;
					} else {
						isMonthUser = false;
					}
				}
			}

		}

		if (!isMonthUser) {
			dataMap.put("uT", "0");
		} else {
			// Monthuser monthuser=monthusers.get(0);
			dataMap.put("uT", "1");
			// 判断是否是预约车
			if (monthUserType == 1) {
				dataMap.put("uT", "2");
			}
		}
		List<Parktoalipark> parktoaliparks = parkToAliparkService.getByParkId(parkId);
		Boolean isMultiCarsOneCarport = false;
		if (park.getDescription() != null && park.getDescription().contains("一位多车")) {
			logger.info(cardNumber + "进入一位多车停车场");
			isMultiCarsOneCarport = true;
		}
		Boolean isMultiFeeCtriterion = false;
		if (park.getDescription() != null && park.getDescription().contains("多计费标准")) {
			isMultiFeeCtriterion = true;
		}
		// 判断是否有多个车
		List<Monthuser> realMonthUsers = new ArrayList<>();
		if (monthuserNow != null && monthuserNow.getCardnumber() != null && !monthuserNow.getCardnumber().equals("")) {
			List<Monthuser> monthuserss = monthUserService.getByParkAndPort(monthuserNow.getParkid(),
					monthuserNow.getCardnumber());
			for (Monthuser monthuser : monthuserss) {
				if (monthuser.getType().intValue() == 0) {
					realMonthUsers.add(monthuser);
				}
			}
		}

		Parknoticeauthority parknoticeauthority = parkNoticeAuthorityService.getByParkId(parkId).get(0);
		// 入口硬件
		if (channelFlag == 1) {
			if (largeCar == true) {
				charge.setIsLargeCar(true);
			}

			charge.setParkDesc(park.getName() + "-临停车");
			// 取得入口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 1) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			// 贵州队列发送
			if (parkId == 334) {
				Map<String, String> guiyang = new HashMap<>();
				guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
				guiyang.put("type", "in");
				guiyang.put("plateNum", cardNumber);
				guiyang.put("enterTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
			}

			switch (monthUserType) {
			case 0:
				charge.setParkDesc(park.getName() + "-包月车");
				charge.setRejectReason(monthusers.get(0).getOwner());
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				charge.setParkDesc(park.getName() + "-临停车");
				if (parkcarauthority.getTemporary() != true) {
					dataMap.put("aT", "0");

				}
				break;
			default:
				break;
			}

			if (!monthusers.isEmpty()) {
				charge.setRejectReason(monthusers.get(0).getOwner());
			}

			dataMap.put("cT", "in");
			charge.setCardNumber(cardNumber);
			charge.setParkId(parkId);

			// 判断包月车已入场

			Boolean isMonthUserCarIn = false;
			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				for (Monthuser tmMonthuser : realMonthUsers) {
					if (tmMonthuser.getPlatecolor() != null & tmMonthuser.getPlatecolor().equals("多车包月入场")) {
						if (!tmMonthuser.getPlatenumber().equals(cardNumber)) {
							isMonthUserCarIn = true;
						}

					}
				}
				monthuserNow.setPlatecolor("多车包月入场");
				if (isMonthUserCarIn) {
					charge.setParkDesc(park.getName() + "-包月转临停");
					monthuserNow.setPlatecolor("包月转为临停");
					dataMap.put("uT", "0");
					try {
						dataMap.remove("ds");
						dataMap.remove("eD");
					} catch (Exception e) {
						// TODO: handle exception
					}

				}
				monthUserService.updateByPrimaryKeySelective(monthuserNow);
			}
			charge.setPosId(mac);
			// charge.setOperatorId(mac);
			charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			int num = 0;
			// redis查询
			try {
				String redisStr = jedisClient.get("carIn" + parkId + charge.getCardNumber());
				if (redisStr != null) {
					num = 1;
					logger.info("redis检测重复入场" + cardNumber + redisStr);
				} else {
					num = chargeSerivce.insert(charge);
					// 清场
					// 清场
					String redisId = jedisClient.get("P-" + park.getId() + "-" + cardNumber);
					if (redisId != null) {
						PosChargeData posChargeDatatmp = chargeDao.getById(Integer.parseInt(redisId));
						if (posChargeDatatmp != null && !posChargeDatatmp.isPaidCompleted()) {
							posChargeDatatmp.setChargeMoney(0);
							posChargeDatatmp.setExitDate1(posChargeDatatmp.getEntranceDate());
							posChargeDatatmp.setPaidCompleted(true);
							posChargeDatatmp.setOperatorId("redis");
							posChargeDatatmp.setRejectReason("平台清场");
							logger.info(cardNumber + "清场" + posChargeDatatmp.getId());
							// chargeSerivce.update(posChargeDatatmp);
							chargeSerivce.deleteById(posChargeDatatmp.getId());
						}

						jedisClient.del("P-" + parkId + "-" + charge.getCardNumber());
						jedisClient.del("query" + charge.getCardNumber());
						logger.info("重新入场 " + cardNumber + " id" + charge.getId());

					}

					jedisClient.set("carIn" + parkId + charge.getCardNumber(), String.valueOf(charge.getId()), 120);
					logger.info(cardNumber + "设置redis值" + charge.getId());
					jedisClient.set("carInTmp" + parkId + charge.getCardNumber(), "tmp", 20);
					jedisClient.set("query" + charge.getCardNumber(), String.valueOf(charge.getId()), 259200);
					jedisClient.set("P-" + parkId + "-" + charge.getCardNumber(), String.valueOf(charge.getId()),
							Constants.carMaxPeriod);
				}
			} catch (Exception e) {
				// TODO: handle exception
				logger.info(cardNumber + "redis异常" + e);
				dataMap.put("aT", "1");
				dataMap.put("cT", "in");
				dataMap.put("uT", "0");
				return Utility.createJsonMsg(1001, dataMap);
				// num = chargeSerivce.insert(charge);
			}
			if (num == 1) {
				// 发送topic队列
				if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())) {
					charge.setEntrance(true);
					ActiveMqService.SendTopicWithMac(charge, String.valueOf(parkId), mac, park.getPortLeftCount(),
							monthUserType);
				}

				try {

					if (!parktoaliparks.isEmpty()) {
						Parktoalipark parktoalipark = parktoaliparks.get(0);
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", parktoalipark.getAliparkingid());
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");

					} else {

					}

					if (parknoticeauthority != null && parknoticeauthority.getAlipay() == true) {
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", "PI1501317472942184881");
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");
					}
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e);
					dataMap.put("aT", "1");
					dataMap.put("cT", "in");
					dataMap.put("uT", "0");
					return Utility.createJsonMsg(1001, dataMap);
				}
				logger.info(cardNumber + "入场成功!" + dataMap.toString());
				ret.put("status", 1001);
			} else {
				ret.put("status", 1002);
			}
			logger.info(cardNumber + "<---离开[hefeitouched]车辆入场/出场接口");
			return Utility.createJsonMsgWithoutMsg(1001, dataMap);
		} else { // 出场硬件
			dataMap.put("cT", "out");
			dataMap.put("pT", "9");
			List<PosChargeData> queryCharges = null;
			String exitDate = (String) args.get("exitDate");
			logger.info(cardNumber + "开始出场");
			if (monthuserNow != null) {
				realMonthUsers.add(monthuserNow);
			}
			// 取得出口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 0) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			try {
				logger.info(cardNumber + "开始计费---->");
				queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
						isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				// System.out.println("出场时间不为空,getDebt计算完毕: "+new

			} catch (Exception e) {
				return Utility.createJsonMsg(1002, e);
			}
			logger.info(cardNumber + "计费结束----<");
			double shouldChargeMoney = 0;
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {// 非现金支付的情况、二次出场、总之是无计费信息的情况
				logger.info(cardNumber + "没查到未缴费记录!");
				FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());

				if (isRealMonthUser && realMonthUsers.size() == 1) {
					logger.info(cardNumber + "包月用户离场，返回" + Utility.createJsonMsgWithoutMsg(1001, dataMap));
					logger.info(cardNumber + "<---离开[hefeitouched]车辆入场/出场接口");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 以下就是查询停车费状态的部分迁移
				logger.info(cardNumber + "开始查找最近一次的缴费记录");
				List<PosChargeData> posChargeDataList = chargeSerivce.getLastRecordWithPark(cardNumber, 1, parkId);
				if (posChargeDataList.isEmpty()) {
					dataMap.put("my", "0.0");
					dataMap.put("eD", "13");
					logger.info(cardNumber + "未找到查找最近一次的缴费记录，设置13分钟停车时长，并返回即可");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				logger.info(cardNumber + "找到查找最近一次的缴费记录");
				PosChargeData posChargeData = posChargeDataList.get(0);
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date());
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
					logger.info(cardNumber + "找到查找最近一次的缴费记录，但是离场时间为空");
				}
				dataMap.put("pT", String.valueOf(posChargeData.getPayType()));
				// 发送到队列

				if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true) {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", posChargeData.getParkDesc());
					argstoali.put("carNumber", posChargeData.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(posChargeData.getEntranceDate()));
					argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
							.format(posChargeData.getExitDate() == null ? new Date() : posChargeData.getExitDate()));
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				}

				// Date payDate = posChargeData.getExitDate();
				// if (posChargeData.getPayType() == 0) {
				// List<Alipayrecord> alipayrecords =
				// alipayrecordService.getByPosChargeId(posChargeData.getId());
				// if (alipayrecords.isEmpty()) {
				// logger.info(cardNumber + "支付宝支付，但是alipayrecords没有找到支付宝支付的记录");
				// return Utility.createJsonMsgWithoutMsg(1003, dataMap);
				// }
				// payDate = alipayrecords.get(0).getDate();
				// } else if (posChargeData.getPayType() == 1) {
				// payDate = posChargeData.getExitDate();
				// }
				// if (payDate == null) {
				// payDate = new Date();
				// }
				Date payDate = posChargeData.getExitDate();
				if (posChargeData.getPayType() == 0 || posChargeData.getPayType() == 1) {
					logger.info(cardNumber + "已经通过移动进行支付");
					List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
					if (alipayrecords.isEmpty()) {
						payDate = new Date();
					} else {
						payDate = alipayrecords.get(0).getDate();
						logger.info(
								cardNumber + "已查询到支付时间!" + new SimpleDateFormat(Constants.DATEFORMAT).format(payDate));
					}

				}
				if (payDate == null) {
					payDate = new Date();
				}
				long diff = new Date().getTime() - payDate.getTime();

				// 防止逃费
				logger.info(cardNumber + "开始执行防逃费功能---->");
				if (posChargeData.getPayType() == 9 && diff < 1000 * 60 * 60) {// 12小时内防逃费
					logger.info(cardNumber + "防逃费，而且时间小于12小时");
					dataMap.put("my", String.valueOf(posChargeData.getPaidMoney()));
					if (posChargeData.getChargeMoney() > 0.1) {
						dataMap.put("aT", "0");
					} else {
						dataMap.put("aT", "1");
					}
					// dataMap.put("eD", String
					// .valueOf((new Date().getTime() - posChargeData.getEntranceDate().getTime()) /
					// (1000 * 60)));
					dataMap.put("eD",
							String.valueOf(
									(posChargeData.getExitDate().getTime() - posChargeData.getEntranceDate().getTime())
											/ (1000 * 60)));
					logger.info(cardNumber + "防逃费:" + dataMap.toString());
					logger.info(cardNumber + "结束执行防逃费功能----<");
					if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
							|| park.getDescription().contains("本地计费")) {
						posChargeData.setEntrance(false);
						ActiveMqService.SendTopicWithMac(posChargeData, String.valueOf(parkId), mac,
								park.getPortLeftCount(), monthUserType);
					}
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				if (diff < 1000 * 60 * 15) {
					logger.info(cardNumber + "属于非现金支付完成后15分钟内离场的情况，正常离场即可");
					dataMap.put("my", "0.0");
					long diff1 = (new Date().getTime() - posChargeData.getEntranceDate().getTime());

					dataMap.put("eD", String.valueOf((diff1 / (1000 * 60)) > 15 ? 13 : (diff1 / (1000 * 60))));
					posChargeData.setPaidCompleted(true);

					posChargeData.setPaidMoney(posChargeData.getChargeMoney());
					posChargeData.setUnPaidMoney(0);
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					chargeSerivce.update(posChargeData);
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 超过了15分钟
				else if (posChargeData.getPayType() != 2 && posChargeData.getPayType() != 9
						&& posChargeData.getPayType() < 20) {
					logger.info(cardNumber + "支付完成超时15分钟出场,将重新生成订单");
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(park.getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					logger.info(cardNumber + "自动生成订单入场时间："
							+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);

					jedisClient.set("P-" + parkId + "-" + charge2.getCardNumber(), String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);

					logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
					parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
					logger.info(cardNumber + "离开" + park.getName() + "后车位数" + park.getPortLeftCount() + 1);

					jedisClient.del("P-" + parkId + "-" + cardNumber);
					jedisClient.del("query" + cardNumber);
					logger.info("支付完超15分钟重新入场 " + cardNumber + " id" + charge2.getId());
					jedisClient.set("P-" + parkId + "-" + cardNumber, String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);
					if (num != 1) {
						return Utility.createJsonMsgWithoutMsg(1003, dataMap);
					}
					// 重新生成订单了，则需要重新计费
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				}
			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(cardNumber + "开始进行现金支付处理逻辑");
			int num = 0;
			if (!queryCharges.isEmpty()) {// 纯现金支付在此处理

				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(mac);
					if (posChargeData.getParkId() == parkId.intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);
						posChargeData.setPayType(9);
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}

						chargeSerivce.update(posChargeData);
					}
				}

				shouldChargeMoney = payRet.getChargeMoney();
				num = chargeSerivce.update(payRet);
			} else {

				dataMap.put("eD", "14");
				dataMap.put("my", "0.0");
				logger.info(cardNumber + "查询计费结果为空,强行将费用设置为0，然后停车时长设置为14分钟");
				return Utility.createJsonMsg(1001, dataMap);
			}

			logger.info(cardNumber + "权限-" + monthUserType);
			switch (monthUserType) {
			case 0:
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");
				}
				if (payRet.getChargeMoney() <= 0) {
					dataMap.put("aT", "1");
				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (parkcarauthority.getTemporary() != true && shouldChargeMoney > 0) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority.getTemporary0() != true && shouldChargeMoney < 0.1) {
					dataMap.put("aT", "0");
				}
				break;
			default:
				break;
			}

			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
					|| park.getDescription().contains("本地计费")) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(parkId), mac, park.getPortLeftCount(),
						monthUserType);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, parkId.intValue())
					|| park.getDescription().contains("工行停车场")) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}

			if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true && !queryCharges.isEmpty()) {

				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getEntranceDate()));
					argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getExitDate()));
					logger.info(cardNumber + "weixinoutargs:" + argstoali.toString());
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(cardNumber + "发送微信出场通知错误：" + e);
				}

			}
			// logger.info(cardNumber + "1369");
			if (payRet.getEntranceDate() == null) {

				return Utility.createJsonMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());
			dataMap.put("eD", String.valueOf((diff2 / (60 * 1000)) > 636000 ? 13 : (diff2 / (60 * 1000))));// ?????????
			if (!isRealMonthUser) {
				dataMap.put("my", String.valueOf(payRet.getPaidMoney()));
			}

			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				if (payRet.getChargeMoney() > 0) {
					dataMap.put("my", String.valueOf(payRet.getChargeMoney()));
					dataMap.put("uT", "0");
				}
			}
			if (payRet.getChargeMoney() > 0) {
				// logger.info(cardNumber+"金额大于0,at设置为0");
				dataMap.put("my", String.valueOf(payRet.getChargeMoney()));
				dataMap.put("aT", "0");
			}
			dataMap.put("pT", String.valueOf(payRet.getPayType()));
			if (num == 1) {
				// Poschargemac poschargemac = new Poschargemac();
				// poschargemac.setMacidout((int) info.get("macId"));
				// poschargemac.setPoschargeid(payRet.getId());
				// posChargeMacService.updateByPosChargeId(poschargemac);
				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());
				// posChargeMacService.insertSelective(poschargemac);
				jedisClient.set("out" + mac, String.valueOf(payRet.getId()));
				logger.info(cardNumber + "出场成功!" + dataMap.toString());

				parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
				logger.info(cardNumber + "<---离开[hefeitouched]车辆入场/出场接口");
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			} else {
				return Utility.createJsonMsg(1001, "ok");
			}
		}
	}

	@RequestMapping(value = "hefeitouchedV2", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public String hefeitouchedV2(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		logger.info(cardNumber + "--->进入[hefeitouched]车辆入场/出场接口");

		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		PosChargeData charge = new PosChargeData();
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1003);
			return Utility.gson.toJson(dataMap);
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			return Utility.createJsonMsg(1002, "fail");
		}
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		// 相似车牌管理
		List<Similarcarnumber> similarcarnumbers = similarCarNumberService.selectBySimilarCarNumberAndPark(cardNumber,
				parkId);
		if (!similarcarnumbers.isEmpty()) {
			cardNumber = similarcarnumbers.get(0).getRealnumber();
			logger.info("更正车牌:" + cardNumber);
		}

		List<Monthuser> monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
		Park park = parkService.getParkById(parkId);
		List<Parkcarauthority> parkcarauthorities = parkCarAuthorityService.getByParkId(parkId);
		if (parkcarauthorities.isEmpty()) {
			return null;
		}

		dataMap.put("aT", "1");
		boolean isMonthUser = false;
		boolean isRealMonthUser = false;
		int monthUserType = 9;
		Monthuser monthuserNow = null;
		if (!monthusers.isEmpty()) {
			isMonthUser = true;
			for (Monthuser tmpMonthuser : monthusers) {
				logger.info(cardNumber + "月卡id:" + tmpMonthuser.getId() + "类型:" + tmpMonthuser.getType() + "到期时间:"
						+ Utility.formatDatetime(tmpMonthuser.getEndtime()));
				Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
				monthuserNow = tmpMonthuser;
				if (tmpMonthuser.getType() == 0) {// 月卡
					monthUserType = 0;
					if (diff > 0) {
						int leftDays = (int) (diff / (1000 * 60 * 60 * 24));
						dataMap.put("ds", String.valueOf(leftDays));
						isRealMonthUser = true;
						dataMap.put("my", "0.0");
						dataMap.put("eD", new SimpleDateFormat(Constants.DATEFORMAT).format(tmpMonthuser.getEndtime()));
						break;
					} else {
						dataMap.put("ds", "-1");
						dataMap.put("uT", "1");
						monthUserType = 8; // 月卡过期
					}

				} else {
					if (diff >= 0) {
						monthUserType = tmpMonthuser.getType();
						break;
					} else {
						isMonthUser = false;
					}
				}
			}

		}

		if (!isMonthUser) {
			dataMap.put("uT", "0");
		} else {
			// Monthuser monthuser=monthusers.get(0);
			dataMap.put("uT", "1");
			// 判断是否是预约车
			if (monthUserType == 1) {
				dataMap.put("uT", "2");
			}
		}
		List<Parktoalipark> parktoaliparks = parkToAliparkService.getByParkId(parkId);
		Boolean isMultiCarsOneCarport = false;
		if (park.getDescription() != null && park.getDescription().contains("一位多车")) {
			logger.info(cardNumber + "进入一位多车停车场");
			isMultiCarsOneCarport = true;
		}
		Boolean isMultiFeeCtriterion = false;
		if (park.getDescription() != null && park.getDescription().contains("多计费标准")) {
			isMultiFeeCtriterion = true;
		}
		// 判断是否有多个车
		List<Monthuser> realMonthUsers = new ArrayList<>();
		if (monthuserNow != null && monthuserNow.getCardnumber() != null && !monthuserNow.getCardnumber().equals("")) {
			List<Monthuser> monthuserss = monthUserService.getByParkAndPort(monthuserNow.getParkid(),
					monthuserNow.getCardnumber());
			for (Monthuser monthuser : monthuserss) {
				if (monthuser.getType().intValue() == 0) {
					realMonthUsers.add(monthuser);
				}
			}
		}

		Parknoticeauthority parknoticeauthority = parkNoticeAuthorityService.getByParkId(parkId).get(0);
		Parkcarauthority2 parkcarauthority2 = parkcarauthority2Service.selectByPark(parkId);

		charge.setParkDesc(park.getName() + "-临停车");

		// 入口硬件
		if (channelFlag == 1) {
			if (largeCar == true) {
				charge.setIsLargeCar(true);
			}

			charge.setParkDesc(park.getName() + "-临停车");
			// 取得入口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 1) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			// 贵州队列发送
			if (parkId == 334) {
				Map<String, String> guiyang = new HashMap<>();
				guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
				guiyang.put("type", "in");
				guiyang.put("plateNum", cardNumber);
				guiyang.put("enterTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
			}

			switch (monthUserType) {
			case 0:
				charge.setParkDesc(park.getName() + "-包月车");
				charge.setRejectReason(monthusers.get(0).getOwner());
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				charge.setParkDesc(park.getName() + "-临停车");
				if (parkcarauthority.getTemporary() != true) {
					dataMap.put("aT", "0");

				}
				break;
			default:
				break;
			}

			if (!monthusers.isEmpty()) {
				charge.setRejectReason(monthusers.get(0).getOwner());
			}

			dataMap.put("cT", "in");
			charge.setCardNumber(cardNumber);
			charge.setParkId(parkId);

			// 判断包月车已入场

			Boolean isMonthUserCarIn = false;
			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				for (Monthuser tmMonthuser : realMonthUsers) {
					if (tmMonthuser.getPlatecolor() != null & tmMonthuser.getPlatecolor().equals("多车包月入场")) {
						if (!tmMonthuser.getPlatenumber().equals(cardNumber)) {
							isMonthUserCarIn = true;
						}

					}
				}
				monthuserNow.setPlatecolor("多车包月入场");
				if (isMonthUserCarIn) {
					charge.setParkDesc(park.getName() + "-包月转临停");
					monthuserNow.setPlatecolor("包月转为临停");
					dataMap.put("uT", "0");
					try {
						dataMap.remove("ds");
						dataMap.remove("eD");
					} catch (Exception e) {
						// TODO: handle exception
					}

				}
				monthUserService.updateByPrimaryKeySelective(monthuserNow);
			}
			charge.setPosId(mac);
			// charge.setOperatorId(mac);
			charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			int num = 0;
			// redis查询
			try {
				String redisStr = jedisClient.get("carIn" + parkId + charge.getCardNumber());
				if (redisStr != null) {
					num = 1;
					logger.info("redis检测重复入场" + cardNumber + redisStr);
				} else {
					num = chargeSerivce.insert(charge);
					// 清场
					// 清场
					String redisId = jedisClient.get("P-" + park.getId() + "-" + cardNumber);
					if (redisId != null) {
						PosChargeData posChargeDatatmp = chargeDao.getById(Integer.parseInt(redisId));
						if (posChargeDatatmp != null && !posChargeDatatmp.isPaidCompleted()) {
							posChargeDatatmp.setChargeMoney(0);
							posChargeDatatmp.setExitDate1(posChargeDatatmp.getEntranceDate());
							posChargeDatatmp.setPaidCompleted(true);
							posChargeDatatmp.setOperatorId("redis");
							posChargeDatatmp.setRejectReason("平台清场");
							logger.info(cardNumber + "清场" + posChargeDatatmp.getId());
							// chargeSerivce.update(posChargeDatatmp);
							chargeSerivce.deleteById(posChargeDatatmp.getId());
						}

						jedisClient.del("P-" + parkId + "-" + charge.getCardNumber());
						jedisClient.del("query" + charge.getCardNumber());
						logger.info("重新入场 " + cardNumber + " id" + charge.getId());

					}

					jedisClient.set("carIn" + parkId + charge.getCardNumber(), String.valueOf(charge.getId()), 120);
					logger.info(cardNumber + "设置redis值" + charge.getId());
					jedisClient.set("carInTmp" + parkId + charge.getCardNumber(), "tmp", 20);
					jedisClient.set("query" + charge.getCardNumber(), String.valueOf(charge.getId()), 259200);
					jedisClient.set("P-" + parkId + "-" + charge.getCardNumber(), String.valueOf(charge.getId()),
							Constants.carMaxPeriod);
				}
			} catch (Exception e) {
				// TODO: handle exception
				logger.info(cardNumber + "redis异常" + e);
				// num = chargeSerivce.insert(charge);
			}
			if (num == 1) {
				// 发送topic队列
				if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
						|| park.getDescription().contains("本地计费")) {
					charge.setEntrance(true);
					ActiveMqService.SendTopicWithMac(charge, String.valueOf(parkId), mac, park.getPortLeftCount(),
							monthUserType);
				}

				try {

					if (!parktoaliparks.isEmpty()) {
						Parktoalipark parktoalipark = parktoaliparks.get(0);
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", parktoalipark.getAliparkingid());
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");

					}
					if (parknoticeauthority != null && parknoticeauthority.getAlipay() == true) {
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", "PI1501317472942184881");
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");
					}
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e);
				}
				logger.info(cardNumber + "入场成功!" + dataMap.toString());
				ret.put("status", 1001);
			} else {
				ret.put("status", 1002);
			}
			logger.info(cardNumber + "<---离开[hefeitouched]车辆入场/出场接口");
			return Utility.createJsonMsgWithoutMsg(1001, dataMap);
		} else { // 出场硬件
			dataMap.put("cT", "out");
			dataMap.put("pT", "9");
			List<PosChargeData> queryCharges = null;
			String exitDate = (String) args.get("exitDate");
			logger.info(cardNumber + "开始出场");
			if (monthuserNow != null) {
				realMonthUsers.add(monthuserNow);
			}
			// 取得出口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 0) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			try {
				logger.info(cardNumber + "开始计费---->");
				queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
						isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				if (queryCharges.isEmpty()) {
					SmartGateController sm = new SmartGateController();
					PosChargeData posChargeData = sm.outParkingFuzzyMatching(parkId, cardNumber);// 车辆出场车牌模糊匹配
					if (posChargeData != null) {
						queryCharges = chargeSerivce.getDebtWithData(posChargeData.getCardNumber(), parktoaliparks,
								realMonthUsers, park, isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
					}

				}
				// System.out.println("出场时间不为空,getDebt计算完毕: "+new

			} catch (Exception e) {
				dataMap.put("aT", "1");
				dataMap.put("cT", "out");
				dataMap.put("eD", "13");
				dataMap.put("my", "0.0");
				dataMap.put("pT", "9");
				dataMap.put("uT", "0");
				return Utility.createJsonMsg(1001, dataMap);
			}
			logger.info(cardNumber + "计费结束----<");
			double shouldChargeMoney = 0;
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {// 非现金支付的情况、二次出场、总之是无计费信息的情况
				logger.info(cardNumber + "没查到未缴费记录!");
				FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());

				if (isRealMonthUser && realMonthUsers.size() == 1) {
					logger.info(cardNumber + "包月用户离场，返回" + Utility.createJsonMsgWithoutMsg(1001, dataMap));
					logger.info(cardNumber + "<---离开[hefeitouched]车辆入场/出场接口");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 以下就是查询停车费状态的部分迁移
				logger.info(cardNumber + "开始查找最近一次的缴费记录");
				List<PosChargeData> posChargeDataList = chargeSerivce.getLastRecordWithPark(cardNumber, 1, parkId);
				if (!posChargeDataList.isEmpty()) {
					PosChargeData tempPos = posChargeDataList.get(0);
					if (tempPos.getExitDate() != null) {// 最近一次记录超过两天则记录作废
						if ((new Date().getTime() - tempPos.getExitDate().getTime()) > 2 * 24 * 60 * 60 * 1000) {
							posChargeDataList = null;
						}
					}
				}
				if (CollectionUtils.isEmpty(posChargeDataList)) {
					dataMap.put("my", "0.0");
					dataMap.put("eD", "13");
					logger.info(cardNumber + "未找到查找最近一次的缴费记录，设置13分钟停车时长，并返回即可");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				logger.info(cardNumber + "找到查找最近一次的缴费记录");
				PosChargeData posChargeData = posChargeDataList.get(0);
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date());
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
					logger.info(cardNumber + "找到查找最近一次的缴费记录，但是离场时间为空");
				}
				dataMap.put("pT", String.valueOf(posChargeData.getPayType()));
				// 发送到队列

				if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true) {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", posChargeData.getParkDesc());
					argstoali.put("carNumber", posChargeData.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(posChargeData.getEntranceDate()));
					argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
							.format(posChargeData.getExitDate() == null ? new Date() : posChargeData.getExitDate()));
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				}

				// Date payDate = posChargeData.getExitDate();
				// if (posChargeData.getPayType() == 0) {
				// List<Alipayrecord> alipayrecords =
				// alipayrecordService.getByPosChargeId(posChargeData.getId());
				// if (alipayrecords.isEmpty()) {
				// logger.info(cardNumber + "支付宝支付，但是alipayrecords没有找到支付宝支付的记录");
				// return Utility.createJsonMsgWithoutMsg(1003, dataMap);
				// }
				// payDate = alipayrecords.get(0).getDate();
				// } else if (posChargeData.getPayType() == 1) {
				// payDate = posChargeData.getExitDate();
				// }
				// if (payDate == null) {
				// payDate = new Date();
				// }
				Date payDate = posChargeData.getExitDate();
				if (posChargeData.getPayType() == 0 || posChargeData.getPayType() == 1) {
					logger.info(cardNumber + "已经通过移动进行支付");
					List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
					if (alipayrecords.isEmpty()) {
						payDate = new Date();
					} else {
						payDate = alipayrecords.get(0).getDate();
						logger.info(
								cardNumber + "已查询到支付时间!" + new SimpleDateFormat(Constants.DATEFORMAT).format(payDate));
					}

				}
				if (payDate == null) {
					payDate = new Date();
				}
				long diff = new Date().getTime() - payDate.getTime();

				// 防止逃费
				logger.info(cardNumber + "开始执行防逃费功能---->");
				if (posChargeData.getPayType() == 9 && diff < 1000 * 60 * 60) {// 12小时内防逃费
					logger.info(cardNumber + "防逃费，而且时间小于12小时");
					dataMap.put("my", String.valueOf(posChargeData.getChargeMoney()));
					if (posChargeData.getChargeMoney() > 0.1) {
						dataMap.put("aT", "0");
					} else {
						dataMap.put("aT", "1");
					}
					// dataMap.put("eD", String
					// .valueOf((new Date().getTime() - posChargeData.getEntranceDate().getTime()) /
					// (1000 * 60)));
					dataMap.put("eD",
							String.valueOf(
									(posChargeData.getExitDate().getTime() - posChargeData.getEntranceDate().getTime())
											/ (1000 * 60)));
					logger.info(cardNumber + "防逃费:" + dataMap.toString());
					logger.info(cardNumber + "结束执行防逃费功能----<");
					if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
							|| park.getDescription().contains("本地计费")) {
						posChargeData.setEntrance(false);
						ActiveMqService.SendTopicWithMac(posChargeData, String.valueOf(parkId), mac,
								park.getPortLeftCount(), monthUserType);
					}
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				if (diff < 1000 * 60 * 15) {
					logger.info(cardNumber + "属于非现金支付完成后15分钟内离场的情况，正常离场即可");
					dataMap.put("my", "0.0");
					long diff1 = (new Date().getTime() - posChargeData.getEntranceDate().getTime());

					dataMap.put("eD", String.valueOf((diff1 / (1000 * 60)) > 15 ? 13 : (diff1 / (1000 * 60))));
					posChargeData.setPaidCompleted(true);

					posChargeData.setPaidMoney(posChargeData.getChargeMoney());
					posChargeData.setUnPaidMoney(0);
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					chargeSerivce.update(posChargeData);
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 超过了15分钟
				else if (posChargeData.getPayType() != 2 && posChargeData.getPayType() != 9
						&& posChargeData.getPayType() < 20) {
					logger.info(cardNumber + "支付完成超时15分钟出场,将重新生成订单");
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(park.getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					logger.info(cardNumber + "自动生成订单入场时间："
							+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);

					jedisClient.set("P-" + parkId + "-" + charge2.getCardNumber(), String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);

					logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
					parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
					logger.info(cardNumber + "离开" + park.getName() + "后车位数" + park.getPortLeftCount() + 1);

					jedisClient.del("P-" + parkId + "-" + cardNumber);
					jedisClient.del("query" + cardNumber);
					logger.info("支付完超15分钟重新入场 " + cardNumber + " id" + charge2.getId());
					jedisClient.set("P-" + parkId + "-" + cardNumber, String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);
					if (num != 1) {
						return Utility.createJsonMsgWithoutMsg(1003, dataMap);
					}
					// 重新生成订单了，则需要重新计费
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				}
			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(cardNumber + "开始进行现金支付处理逻辑");
			int num = 0;
			if (!queryCharges.isEmpty()) {// 纯现金支付在此处理

				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(mac);
					if (posChargeData.getParkId() == parkId.intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);
						posChargeData.setPayType(9);
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}

						chargeSerivce.update(posChargeData);
					}
				}

				shouldChargeMoney = payRet.getChargeMoney();
				num = chargeSerivce.update(payRet);
			} else {

				dataMap.put("eD", "14");
				dataMap.put("my", "0.0");
				logger.info(cardNumber + "查询计费结果为空,强行将费用设置为0，然后停车时长设置为14分钟");
				return Utility.createJsonMsg(1001, dataMap);
			}

			logger.info(cardNumber + "权限-" + monthUserType);
			switch (monthUserType) {
			case 0:
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");
				}
				if (payRet.getChargeMoney() <= 0) {
					dataMap.put("aT", "1");
				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (parkcarauthority.getTemporary() != true && shouldChargeMoney > 0) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority.getTemporary0() != true && shouldChargeMoney < 0.1) {
					dataMap.put("aT", "0");
				}
				break;
			default:
				break;
			}

			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, parkId.intValue())
					|| park.getDescription().contains("工行停车场")) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}

			if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true && !queryCharges.isEmpty()) {

				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getEntranceDate()));
					argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getExitDate()));
					logger.info(cardNumber + "weixinoutargs:" + argstoali.toString());
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(cardNumber + "发送微信出场通知错误：" + e);
				}

			}
			// logger.info(cardNumber + "1369");
			if (payRet.getEntranceDate() == null) {

				return Utility.createJsonMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());
			dataMap.put("eD", String.valueOf((diff2 / (60 * 1000)) > 636000 ? 13 : (diff2 / (60 * 1000))));// ?????????
			if (!isRealMonthUser) {
				dataMap.put("my", String.valueOf(payRet.getChargeMoney()));
			}

			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				if (payRet.getChargeMoney() > 0) {
					dataMap.put("my", String.valueOf(payRet.getChargeMoney()));
					dataMap.put("uT", "0");
				}
			}
			if (payRet.getChargeMoney() > 0) {
				// logger.info(cardNumber+"金额大于0,at设置为0");
				dataMap.put("my", String.valueOf(payRet.getChargeMoney()));
				dataMap.put("aT", "0");
			}
			dataMap.put("pT", String.valueOf(payRet.getPayType()));
			if (num == 1) {
				// Poschargemac poschargemac = new Poschargemac();
				// poschargemac.setMacidout((int) info.get("macId"));
				// poschargemac.setPoschargeid(payRet.getId());
				// posChargeMacService.updateByPosChargeId(poschargemac);
				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());
				// posChargeMacService.insertSelective(poschargemac);
				jedisClient.set("out" + mac, String.valueOf(payRet.getId()));
				logger.info(cardNumber + "出场成功!" + dataMap.toString());

				parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
				logger.info(cardNumber + "<---离开[hefeitouched]车辆入场/出场接口");
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			} else {
				return Utility.createJsonMsg(1001, "ok");
			}
		}
	}

	@RequestMapping(value = "testhefeitouched", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public String testhefeitouched(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		logger.info(cardNumber + "--->进入[hefeitouched]车辆入场/出场接口");

		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		PosChargeData charge = new PosChargeData();
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1003);
			return Utility.gson.toJson(dataMap);
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			return Utility.createJsonMsg(1002, "fail");
		}
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		// 相似车牌管理
		List<Similarcarnumber> similarcarnumbers = similarCarNumberService.selectBySimilarCarNumberAndPark(cardNumber,
				parkId);
		if (!similarcarnumbers.isEmpty()) {
			cardNumber = similarcarnumbers.get(0).getRealnumber();
			logger.info("更正车牌:" + cardNumber);
		}

		List<Monthuser> monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
		Park park = parkService.getParkById(parkId);
		List<Parkcarauthority> parkcarauthorities = parkCarAuthorityService.getByParkId(parkId);
		if (parkcarauthorities.isEmpty()) {
			return null;
		}

		dataMap.put("aT", "1");
		boolean isMonthUser = false;
		boolean isRealMonthUser = false;
		int monthUserType = 9;
		Monthuser monthuserNow = null;
		if (!monthusers.isEmpty()) {
			isMonthUser = true;
			for (Monthuser tmpMonthuser : monthusers) {
				logger.info(cardNumber + "月卡id:" + tmpMonthuser.getId() + "类型:" + tmpMonthuser.getType() + "到期时间:"
						+ Utility.formatDatetime(tmpMonthuser.getEndtime()));
				Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
				monthuserNow = tmpMonthuser;
				if (tmpMonthuser.getType() == 0) {// 月卡
					monthUserType = 0;
					if (diff > 0) {
						int leftDays = (int) (diff / (1000 * 60 * 60 * 24));
						dataMap.put("ds", String.valueOf(leftDays));
						isRealMonthUser = true;
						dataMap.put("eD", new SimpleDateFormat(Constants.DATEFORMAT).format(tmpMonthuser.getEndtime()));
						break;
					} else {
						dataMap.put("ds", "-1");
						dataMap.put("uT", "1");
						monthUserType = 8; // 月卡过期
					}

				} else {
					if (diff >= 0) {
						monthUserType = tmpMonthuser.getType();
						break;
					} else {
						isMonthUser = false;
					}
				}
			}

		}

		if (!isMonthUser) {
			dataMap.put("uT", "0");
		} else {
			// Monthuser monthuser=monthusers.get(0);
			dataMap.put("uT", "1");
			// 判断是否是预约车
			if (monthUserType == 1) {
				dataMap.put("uT", "2");
			}
		}
		List<Parktoalipark> parktoaliparks = parkToAliparkService.getByParkId(parkId);
		Boolean isMultiCarsOneCarport = false;
		if (park.getDescription() != null && park.getDescription().contains("一位多车")) {
			logger.info(cardNumber + "进入一位多车停车场");
			isMultiCarsOneCarport = true;
		}
		Boolean isMultiFeeCtriterion = false;
		if (park.getDescription() != null && park.getDescription().contains("多计费标准")) {
			isMultiFeeCtriterion = true;
		}
		// 判断是否有多个车
		List<Monthuser> realMonthUsers = new ArrayList<>();
		if (monthuserNow != null && monthuserNow.getCardnumber() != null && !monthuserNow.getCardnumber().equals("")) {
			List<Monthuser> monthuserss = monthUserService.getByParkAndPort(monthuserNow.getParkid(),
					monthuserNow.getCardnumber());
			for (Monthuser monthuser : monthuserss) {
				if (monthuser.getType().intValue() == 0) {
					realMonthUsers.add(monthuser);
				}
			}
		}

		Parknoticeauthority parknoticeauthority = parkNoticeAuthorityService.getByParkId(parkId).get(0);
		Parkcarauthority2 parkcarauthority2 = parkcarauthority2Service.selectByPark(parkId);

		charge.setParkDesc(park.getName() + "-临停车");

		// 入口硬件
		if (channelFlag == 1) {
			if (largeCar == true) {
				charge.setIsLargeCar(true);
			}

			charge.setParkDesc(park.getName() + "-临停车");
			// 取得入口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 1) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			// 贵州队列发送
			if (parkId == 334) {
				Map<String, String> guiyang = new HashMap<>();
				guiyang.put("parkId", "f664ebf8-e89c-4c8d-8559-94bce30f42de");
				guiyang.put("type", "in");
				guiyang.put("plateNum", cardNumber);
				guiyang.put("enterTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(guiyang), "guizhou");
			}

			switch (monthUserType) {
			case 0:
				charge.setParkDesc(park.getName() + "-包月车");
				charge.setRejectReason(monthusers.get(0).getOwner());
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				charge.setParkDesc(park.getName() + "-临停车");
				if (parkcarauthority.getTemporary() != true) {
					dataMap.put("aT", "0");

				}
				break;
			default:
				break;
			}

			if (!monthusers.isEmpty()) {
				charge.setRejectReason(monthusers.get(0).getOwner());
			}

			dataMap.put("cT", "in");
			charge.setCardNumber(cardNumber);
			charge.setParkId(parkId);

			// 判断包月车已入场

			Boolean isMonthUserCarIn = false;
			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				for (Monthuser tmMonthuser : realMonthUsers) {
					if (tmMonthuser.getPlatecolor() != null & tmMonthuser.getPlatecolor().equals("多车包月入场")) {
						if (!tmMonthuser.getPlatenumber().equals(cardNumber)) {
							isMonthUserCarIn = true;
						}

					}
				}
				monthuserNow.setPlatecolor("多车包月入场");
				if (isMonthUserCarIn) {
					charge.setParkDesc(park.getName() + "-包月转临停");
					monthuserNow.setPlatecolor("包月转为临停");
					dataMap.put("uT", "0");
					try {
						dataMap.remove("ds");
						dataMap.remove("eD");
					} catch (Exception e) {
						// TODO: handle exception
					}

				}
				monthUserService.updateByPrimaryKeySelective(monthuserNow);
			}
			charge.setPosId(mac);
			// charge.setOperatorId(mac);
			charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			int num = 0;
			// redis查询
			try {
				String redisStr = jedisClient.get("carIn" + parkId + charge.getCardNumber());
				if (redisStr != null) {
					num = 1;
					logger.info("redis检测重复入场" + cardNumber + redisStr);
				} else {
					num = chargeSerivce.insert(charge);
					// 清场
					// 清场
					String redisId = jedisClient.get("P-" + park.getId() + "-" + cardNumber);
					if (redisId != null) {
						PosChargeData posChargeDatatmp = chargeDao.getById(Integer.parseInt(redisId));
						if (!posChargeDatatmp.isPaidCompleted()) {
							posChargeDatatmp.setChargeMoney(0);
							posChargeDatatmp.setExitDate1(posChargeDatatmp.getEntranceDate());
							posChargeDatatmp.setPaidCompleted(true);
							posChargeDatatmp.setOperatorId("redis");
							posChargeDatatmp.setRejectReason("平台清场");
							logger.info(cardNumber + "清场" + posChargeDatatmp.getId());
							// chargeSerivce.update(posChargeDatatmp);
							chargeSerivce.deleteById(posChargeDatatmp.getId());
						}

						jedisClient.del("P-" + parkId + "-" + charge.getCardNumber());
						jedisClient.del("query" + charge.getCardNumber());
						logger.info("重新入场 " + cardNumber + " id" + charge.getId());

					}

					jedisClient.set("carIn" + parkId + charge.getCardNumber(), String.valueOf(charge.getId()), 120);
					logger.info(cardNumber + "设置redis值" + charge.getId());
					jedisClient.set("carInTmp" + parkId + charge.getCardNumber(), "tmp", 20);
					jedisClient.set("query" + charge.getCardNumber(), String.valueOf(charge.getId()), 259200);
					jedisClient.set("P-" + parkId + "-" + charge.getCardNumber(), String.valueOf(charge.getId()),
							Constants.carMaxPeriod);
				}
			} catch (Exception e) {
				// TODO: handle exception
				logger.info(cardNumber + "redis异常" + e);
				// num = chargeSerivce.insert(charge);
			}
			if (num == 1) {
				// 发送topic队列
				if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
						|| park.getDescription().contains("本地计费")) {
					charge.setEntrance(true);
					ActiveMqService.SendTopicWithMac(charge, String.valueOf(parkId), mac, park.getPortLeftCount(),
							monthUserType);
				}

				try {

					if (!parktoaliparks.isEmpty()) {
						Parktoalipark parktoalipark = parktoaliparks.get(0);
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", parktoalipark.getAliparkingid());
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");

					}
					if (parknoticeauthority != null && parknoticeauthority.getAlipay() == true) {
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", "PI1501317472942184881");
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");
					}
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e);
				}
				logger.info(cardNumber + "入场成功!" + dataMap.toString());
				ret.put("status", 1001);
			} else {
				ret.put("status", 1002);
			}
			logger.info(cardNumber + "<---离开[hefeitouched]车辆入场/出场接口");
			return Utility.createJsonMsgWithoutMsg(1001, dataMap);
		} else { // 出场硬件
			dataMap.put("cT", "out");
			dataMap.put("pT", "9");
			List<PosChargeData> queryCharges = null;
			String exitDate = (String) args.get("exitDate");
			logger.info(cardNumber + "开始出场");
			if (monthuserNow != null) {
				realMonthUsers.add(monthuserNow);
			}
			// 取得出口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 0) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			try {
				logger.info(cardNumber + "开始计费---->");
				queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
						isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				// if (queryCharges.isEmpty() && outParkingFuzzyMatching(parkId, cardNumber) !=
				// null) {//车辆出场车牌模糊匹配
				//
				// queryCharges = chargeSerivce.getDebtWithData(
				// outParkingFuzzyMatching(parkId, cardNumber).getCardNumber(), parktoaliparks,
				// realMonthUsers, park, isMultiFeeCtriterion, monthUserType, false);
				//
				// }
				// System.out.println("出场时间不为空,getDebt计算完毕: "+new

			} catch (Exception e) {
				return Utility.createJsonMsg(1002, e);
			}
			logger.info(cardNumber + "计费结束----<");
			double shouldChargeMoney = 0;
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {// 非现金支付的情况、二次出场、总之是无计费信息的情况
				logger.info(cardNumber + "没查到未缴费记录!");
				FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());

				if (isRealMonthUser && realMonthUsers.size() == 1) {
					logger.info(cardNumber + "包月用户离场，返回" + Utility.createJsonMsgWithoutMsg(1001, dataMap));
					logger.info(cardNumber + "<---离开[hefeitouched]车辆入场/出场接口");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 以下就是查询停车费状态的部分迁移
				logger.info(cardNumber + "开始查找最近一次的缴费记录");
				List<PosChargeData> posChargeDataList = chargeSerivce.getLastRecordWithPark(cardNumber, 1, parkId);
				if (!posChargeDataList.isEmpty()) {
					PosChargeData tempPos = posChargeDataList.get(0);
					if (tempPos.getExitDate() != null) {// 最近一次记录超过两天则记录作废
						if ((new Date().getTime() - tempPos.getExitDate().getTime()) > 2 * 24 * 60 * 60 * 1000) {
							posChargeDataList = null;
						}
					}
				}
				if (CollectionUtils.isEmpty(posChargeDataList)) {
					dataMap.put("my", "0.0");
					dataMap.put("eD", "13");
					logger.info(cardNumber + "未找到查找最近一次的缴费记录，设置13分钟停车时长，并返回即可");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				logger.info(cardNumber + "找到查找最近一次的缴费记录");
				PosChargeData posChargeData = posChargeDataList.get(0);
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date());
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
					logger.info(cardNumber + "找到查找最近一次的缴费记录，但是离场时间为空");
				}
				dataMap.put("pT", String.valueOf(posChargeData.getPayType()));
				// 发送到队列

				if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true) {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", posChargeData.getParkDesc());
					argstoali.put("carNumber", posChargeData.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(posChargeData.getEntranceDate()));
					argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
							.format(posChargeData.getExitDate() == null ? new Date() : posChargeData.getExitDate()));
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				}

				// Date payDate = posChargeData.getExitDate();
				// if (posChargeData.getPayType() == 0) {
				// List<Alipayrecord> alipayrecords =
				// alipayrecordService.getByPosChargeId(posChargeData.getId());
				// if (alipayrecords.isEmpty()) {
				// logger.info(cardNumber + "支付宝支付，但是alipayrecords没有找到支付宝支付的记录");
				// return Utility.createJsonMsgWithoutMsg(1003, dataMap);
				// }
				// payDate = alipayrecords.get(0).getDate();
				// } else if (posChargeData.getPayType() == 1) {
				// payDate = posChargeData.getExitDate();
				// }
				// if (payDate == null) {
				// payDate = new Date();
				// }
				Date payDate = posChargeData.getExitDate();
				if (posChargeData.getPayType() == 0 || posChargeData.getPayType() == 1) {
					logger.info(cardNumber + "已经通过移动进行支付");
					List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
					if (alipayrecords.isEmpty()) {
						payDate = new Date();
					} else {
						payDate = alipayrecords.get(0).getDate();
						logger.info(
								cardNumber + "已查询到支付时间!" + new SimpleDateFormat(Constants.DATEFORMAT).format(payDate));
					}

				}
				if (payDate == null) {
					payDate = new Date();
				}
				long diff = new Date().getTime() - payDate.getTime();

				// 防止逃费
				logger.info(cardNumber + "开始执行防逃费功能---->");
				if (posChargeData.getPayType() == 9 && diff < 1000 * 60 * 60) {// 12小时内防逃费
					logger.info(cardNumber + "防逃费，而且时间小于12小时");
					dataMap.put("my", String.valueOf(posChargeData.getChargeMoney()));
					if (posChargeData.getChargeMoney() > 0.1) {
						dataMap.put("aT", "0");
					} else {
						dataMap.put("aT", "1");
					}
					// dataMap.put("eD", String
					// .valueOf((new Date().getTime() - posChargeData.getEntranceDate().getTime()) /
					// (1000 * 60)));
					dataMap.put("eD",
							String.valueOf(
									(posChargeData.getExitDate().getTime() - posChargeData.getEntranceDate().getTime())
											/ (1000 * 60)));
					logger.info(cardNumber + "防逃费:" + dataMap.toString());
					logger.info(cardNumber + "结束执行防逃费功能----<");
					if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
							|| park.getDescription().contains("本地计费")) {
						posChargeData.setEntrance(false);
						ActiveMqService.SendTopicWithMac(posChargeData, String.valueOf(parkId), mac,
								park.getPortLeftCount(), monthUserType);
					}
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				if (diff < 1000 * 60 * 15) {
					logger.info(cardNumber + "属于非现金支付完成后15分钟内离场的情况，正常离场即可");
					dataMap.put("my", "0.0");
					long diff1 = (new Date().getTime() - posChargeData.getEntranceDate().getTime());

					dataMap.put("eD", String.valueOf((diff1 / (1000 * 60)) > 15 ? 13 : (diff1 / (1000 * 60))));
					posChargeData.setPaidCompleted(true);

					posChargeData.setPaidMoney(posChargeData.getChargeMoney());
					posChargeData.setUnPaidMoney(0);
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					chargeSerivce.update(posChargeData);
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 超过了15分钟
				else if (posChargeData.getPayType() != 2 && posChargeData.getPayType() != 9
						&& posChargeData.getPayType() < 20) {
					logger.info(cardNumber + "支付完成超时15分钟出场,将重新生成订单");
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(park.getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					logger.info(cardNumber + "自动生成订单入场时间："
							+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);

					jedisClient.set("P-" + parkId + "-" + charge2.getCardNumber(), String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);

					logger.info(cardNumber + "离开" + park.getName() + "前车位数" + park.getPortLeftCount());
					parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
					logger.info(cardNumber + "离开" + park.getName() + "后车位数" + park.getPortLeftCount() + 1);

					jedisClient.del("P-" + parkId + "-" + cardNumber);
					jedisClient.del("query" + cardNumber);
					logger.info("支付完超15分钟重新入场 " + cardNumber + " id" + charge2.getId());
					jedisClient.set("P-" + parkId + "-" + cardNumber, String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);
					if (num != 1) {
						return Utility.createJsonMsgWithoutMsg(1003, dataMap);
					}
					// 重新生成订单了，则需要重新计费
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				}
			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(cardNumber + "开始进行现金支付处理逻辑");
			int num = 0;
			if (!queryCharges.isEmpty()) {// 纯现金支付在此处理

				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(mac);
					if (posChargeData.getParkId() == parkId.intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);
						posChargeData.setPayType(9);
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}

						chargeSerivce.update(posChargeData);
					}
				}

				shouldChargeMoney = payRet.getChargeMoney();
				num = chargeSerivce.update(payRet);
			} else {

				dataMap.put("eD", "14");
				dataMap.put("my", "0.0");
				logger.info(cardNumber + "查询计费结果为空,强行将费用设置为0，然后停车时长设置为14分钟");
				return Utility.createJsonMsg(1001, dataMap);
			}

			logger.info(cardNumber + "权限-" + monthUserType);
			switch (monthUserType) {
			case 0:
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");
				}
				if (payRet.getChargeMoney() <= 0) {
					dataMap.put("aT", "1");
				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (parkcarauthority.getTemporary() != true && shouldChargeMoney > 0) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority.getTemporary0() != true && shouldChargeMoney < 0.1) {
					dataMap.put("aT", "0");
				}
				break;
			default:
				break;
			}

			// 发送到topic
			if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
					|| park.getDescription().contains("本地计费")) {
				payRet.setEntrance(false);
				ActiveMqService.SendTopicWithMac(payRet, String.valueOf(parkId), mac, park.getPortLeftCount(),
						monthUserType);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, parkId.intValue())
					|| park.getDescription().contains("工行停车场")) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}

			if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true && !queryCharges.isEmpty()) {

				try {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", payRet.getParkDesc());
					argstoali.put("carNumber", payRet.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getEntranceDate()));
					argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getExitDate()));
					logger.info(cardNumber + "weixinoutargs:" + argstoali.toString());
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				} catch (Exception e) {
					// TODO: handle exception
					logger.info(cardNumber + "发送微信出场通知错误：" + e);
				}

			}
			// logger.info(cardNumber + "1369");
			if (payRet.getEntranceDate() == null) {

				return Utility.createJsonMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());
			dataMap.put("eD", String.valueOf((diff2 / (60 * 1000)) > 636000 ? 13 : (diff2 / (60 * 1000))));// ?????????
			if (!isRealMonthUser) {
				dataMap.put("my", String.valueOf(payRet.getChargeMoney()));
			}

			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				if (payRet.getChargeMoney() > 0) {
					dataMap.put("my", String.valueOf(payRet.getChargeMoney()));
					dataMap.put("uT", "0");
				}
			}
			if (payRet.getChargeMoney() > 0) {
				// logger.info(cardNumber+"金额大于0,at设置为0");
				dataMap.put("my", String.valueOf(payRet.getChargeMoney()));
				dataMap.put("aT", "0");
			}
			dataMap.put("pT", String.valueOf(payRet.getPayType()));
			if (num == 1) {
				// Poschargemac poschargemac = new Poschargemac();
				// poschargemac.setMacidout((int) info.get("macId"));
				// poschargemac.setPoschargeid(payRet.getId());
				// posChargeMacService.updateByPosChargeId(poschargemac);
				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());
				// posChargeMacService.insertSelective(poschargemac);
				jedisClient.set("out" + mac, String.valueOf(payRet.getId()));
				logger.info(cardNumber + "出场成功!" + dataMap.toString());

				parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
				logger.info(cardNumber + "<---离开[hefeitouched]车辆入场/出场接口");
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			} else {
				return Utility.createJsonMsg(1001, "ok");
			}
		}
	}

	// 无感支付
	@RequestMapping(value = "gonghangtouched", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	@ResponseBody
	public String gonghangtouched(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		logger.info("gonghangtouched车辆" + cardNumber);

		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		PosChargeData charge = new PosChargeData();
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> dataMap = new TreeMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			dataMap.put("status", 1003);
			return Utility.gson.toJson(dataMap);
		}
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			return Utility.createJsonMsg(1002, "fail");
		}
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		// 相似车牌管理
		List<Similarcarnumber> similarcarnumbers = similarCarNumberService.selectBySimilarCarNumberAndPark(cardNumber,
				parkId);
		if (!similarcarnumbers.isEmpty()) {
			cardNumber = similarcarnumbers.get(0).getRealnumber();
			logger.info("更正车牌:" + cardNumber);
		}

		List<Monthuser> monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
		Park park = parkService.getParkById(parkId);
		List<Parkcarauthority> parkcarauthorities = parkCarAuthorityService.getByParkId(parkId);
		if (parkcarauthorities.isEmpty()) {
			return null;
		}

		dataMap.put("aT", "1");
		boolean isMonthUser = false;
		boolean isRealMonthUser = false;
		int monthUserType = 9;
		Monthuser monthuserNow = null;
		if (!monthusers.isEmpty()) {
			isMonthUser = true;
			for (Monthuser tmpMonthuser : monthusers) {
				logger.info(cardNumber + "月卡id:" + tmpMonthuser.getId() + "类型:" + tmpMonthuser.getType() + "到期时间:"
						+ Utility.formatDatetime(tmpMonthuser.getEndtime()));
				Long diff = (tmpMonthuser.getEndtime().getTime() - (new Date()).getTime());
				monthuserNow = tmpMonthuser;
				if (tmpMonthuser.getType() == 0) {// 月卡
					monthUserType = 0;
					if (diff > 0) {
						int leftDays = (int) (diff / (1000 * 60 * 60 * 24));
						dataMap.put("ds", String.valueOf(leftDays));
						isRealMonthUser = true;
						dataMap.put("eD", new SimpleDateFormat(Constants.DATEFORMAT).format(tmpMonthuser.getEndtime()));
						break;
					} else {
						dataMap.put("ds", "-1");
						dataMap.put("uT", "1");
						monthUserType = 8; // 月卡过期
					}

				} else {
					if (diff >= 0) {
						monthUserType = tmpMonthuser.getType();
						break;
					} else {
						isMonthUser = false;
					}
				}
			}

		}

		if (!isMonthUser) {
			dataMap.put("uT", "0");
		} else {
			dataMap.put("uT", "1");
			// 判断是否是预约车
			if (monthUserType == 1) {
				dataMap.put("uT", "2");
			}
		}
		List<Parktoalipark> parktoaliparks = parkToAliparkService.getByParkId(parkId);
		Boolean isMultiCarsOneCarport = false;
		if (park.getDescription() != null && park.getDescription().contains("一位多车")) {
			logger.info(cardNumber + "进入一位多车停车场");
			isMultiCarsOneCarport = true;
		}
		Boolean isMultiFeeCtriterion = false;
		if (park.getDescription() != null && park.getDescription().contains("多计费标准")) {
			isMultiFeeCtriterion = true;
		}
		// 判断是否有多个车
		List<Monthuser> realMonthUsers = new ArrayList<>();
		if (monthuserNow != null && monthuserNow.getCardnumber() != null && !monthuserNow.getCardnumber().equals("")) {
			List<Monthuser> monthuserss = monthUserService.getByParkAndPort(monthuserNow.getParkid(),
					monthuserNow.getCardnumber());
			for (Monthuser monthuser : monthuserss) {
				if (monthuser.getType().intValue() == 0) {
					realMonthUsers.add(monthuser);
				}
			}
		}

		Parknoticeauthority parknoticeauthority = parkNoticeAuthorityService.getByParkId(parkId).get(0);
		// 入口硬件
		if (channelFlag == 1) {
			if (largeCar == true) {
				charge.setIsLargeCar(true);
			}

			charge.setParkDesc(park.getName() + "-临停车");
			// 取得入口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 1) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}
			switch (monthUserType) {
			case 0:
				charge.setParkDesc(park.getName() + "-包月车");
				charge.setRejectReason(monthusers.get(0).getOwner());
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				charge.setParkDesc(park.getName() + "-临停车");
				if (parkcarauthority.getTemporary() != true) {
					dataMap.put("aT", "0");

				}
				break;
			default:
				break;
			}

			dataMap.put("cT", "in");
			charge.setCardNumber(cardNumber);
			charge.setParkId(parkId);

			// 判断包月车已入场

			Boolean isMonthUserCarIn = false;
			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				for (Monthuser tmMonthuser : realMonthUsers) {
					if (tmMonthuser.getPlatecolor() != null && tmMonthuser.getPlatecolor().equals("多车包月入场")) {
						if (!tmMonthuser.getPlatenumber().equals(cardNumber)) {
							isMonthUserCarIn = true;
						}

					}
				}
				monthuserNow.setPlatecolor("多车包月入场");
				if (isMonthUserCarIn) {
					charge.setParkDesc(park.getName() + "-包月转临停");
					monthuserNow.setPlatecolor("包月转为临停");
					dataMap.put("uT", "0");
					try {
						dataMap.remove("ds");
						dataMap.remove("eD");
					} catch (Exception e) {
						// TODO: handle exception
					}

				}
				monthUserService.updateByPrimaryKeySelective(monthuserNow);
			}
			charge.setPosId(mac);
			charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			// redis查询
			int num = 0;
			try {
				String redisStr = jedisClient.get("carIn" + parkId + charge.getCardNumber());
				if (redisStr != null) {
					num = 1;
					logger.info("redis检测重复入场" + cardNumber + redisStr);
				} else {
					num = chargeSerivce.insert(charge);
					// 清场
					String redisId = jedisClient.get("P-" + park.getId() + "-" + cardNumber);
					if (redisId != null) {
						PosChargeData posChargeDatatmp = chargeDao.getById(Integer.parseInt(redisId));
						posChargeDatatmp.setChargeMoney(0);
						posChargeDatatmp.setExitDate1(posChargeDatatmp.getEntranceDate());
						posChargeDatatmp.setPaidCompleted(true);
						posChargeDatatmp.setOperatorId("redis");
						posChargeDatatmp.setRejectReason("平台清场r");
						logger.info(cardNumber + "清场-" + posChargeDatatmp.getId());
						// chargeSerivce.update(posChargeDatatmp);
						chargeSerivce.deleteById(posChargeDatatmp.getId());
					}

					jedisClient.set("carIn" + parkId + charge.getCardNumber(), String.valueOf(charge.getId()), 120);
					jedisClient.del("P-" + parkId + "-" + charge.getCardNumber());
					logger.info("重新入场 " + cardNumber + " id" + charge.getId());
					jedisClient.set("P-" + parkId + "-" + charge.getCardNumber(), String.valueOf(charge.getId()),
							Constants.carMaxPeriod);
				}
			} catch (Exception e) {
				// TODO: handle exception
				num = chargeSerivce.insert(charge);
			}
			if (num == 1) {
				try {

					if (!parktoaliparks.isEmpty()) {
						Parktoalipark parktoalipark = parktoaliparks.get(0);
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", parktoalipark.getAliparkingid());
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");

					}

					if (parknoticeauthority != null && parknoticeauthority.getAlipay() == true) {
						Map<String, String> argstoali = new HashMap<>();
						argstoali.put("parking_id", "PI1501317472942184881");
						argstoali.put("car_number", cardNumber);
						argstoali.put("in_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
						ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "aliEnterInfo");
					}

				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e);
				}
				logger.info(cardNumber + "入场成功!" + dataMap.toString());
				ret.put("status", 1001);
			} else {
				ret.put("status", 1002);
			}
			return Utility.createJsonMsgWithoutMsg(1001, dataMap);
		} else { // 出场硬件
			dataMap.put("cT", "out");
			dataMap.put("pT", "9");
			List<PosChargeData> queryCharges = null;
			String exitDate = (String) args.get("exitDate");
			logger.info(cardNumber + "开始出场");
			if (monthuserNow != null) {
				realMonthUsers.add(monthuserNow);
			}

			// 取得出口权限
			Parkcarauthority parkcarauthority = parkcarauthorities.get(0);
			for (Parkcarauthority tmParkcarauthority : parkcarauthorities) {
				if (tmParkcarauthority.getChannel() == 0) {
					parkcarauthority = tmParkcarauthority;
					break;
				}
			}

			if (exitDate != null) {
				// Date eDate = new
				// SimpleDateFormat(Constants.DATEFORMAT).parse(exitDate);
				try {
					// System.out.println("出场时间为空,将要进行getDebt计算: "+new
					// Date().getTime()+"\n");
					// queryCharges = chargeSerivce.getDebt(cardNumber, eDate);
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);

					// System.out.println("出场时间为空,getDebt计算完毕: "+new
					// Date().getTime()+"\n");

				} catch (Exception e) {
					// TODO Auto-generated catch block
					return Utility.createJsonMsg(1002, "获取费用出现异常");
				}
			} else {
				try {
					// System.out.println("出场时间不为空,将要进行getDebt计算: "+new
					// Date().getTime()+"\n");
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
					// System.out.println("出场时间不为空,getDebt计算完毕: "+new
					// Date().getTime()+"\n");
				} catch (Exception e) {
					return Utility.createJsonMsg(1002, e);
				}
			}
			logger.info(cardNumber + "计费结束!");
			double shouldChargeMoney = 0;
			// 如果没有未缴费 判断最近一次缴费时间是否超过15分钟
			if (queryCharges.isEmpty()) {
				logger.info(cardNumber + "没有未缴费!");
				FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());

				if (isRealMonthUser && realMonthUsers.size() == 1) {
					// dataMap.put("my", "0.0");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 以下就是查询停车费状态的部分迁移
				List<PosChargeData> posChargeDataList = chargeSerivce.getLastRecordWithPark(cardNumber, 1, parkId);
				if (posChargeDataList.isEmpty()) {
					dataMap.put("my", "0.0");
					dataMap.put("eD", "13");
					logger.info(cardNumber + "未找到查找最近一次的缴费记录，设置13分钟停车时长，并返回即可");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				PosChargeData posChargeData = posChargeDataList.get(0);
				if (posChargeData.getExitDate() == null
						&& (posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9)) {
					posChargeData.setExitDate1(new Date(posChargeData.getEntranceDate().getTime() + 1000 * 60 * 13));
					posChargeData.setOther2("未检出时间 默认13分钟" + System.currentTimeMillis());
				}

				// 发送到队列

				if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true) {
					Map<String, String> argstoali = new HashMap<>();
					argstoali.put("parkName", posChargeData.getParkDesc());
					argstoali.put("carNumber", posChargeData.getCardNumber());
					argstoali.put("enterTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(posChargeData.getEntranceDate()));
					argstoali.put("exitTime",
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(posChargeData.getExitDate()));
					ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
				}

				Date payDate = new Date();
				if (posChargeData.getPayType() == 0) {
					List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
					if (alipayrecords.isEmpty()) {
						// dataMap.put("my", "0.0");
						return Utility.createJsonMsgWithoutMsg(1003, dataMap);
					}
					payDate = alipayrecords.get(0).getDate();
				} else if (posChargeData.getPayType() == 1) {
					payDate = posChargeData.getExitDate();
				}
				if (payDate == null) {
					payDate = new Date();
				}

				long diff = new Date().getTime() - payDate.getTime();

				// 防止逃费
				if (posChargeData.getPayType() == 9 && diff < 1000 * 60 * 60) {
					dataMap.put("my", String.valueOf(posChargeData.getChargeMoney()));
					if (posChargeData.getChargeMoney() > 0.1) {
						dataMap.put("aT", "0");
					} else {
						dataMap.put("aT", "1");
					}
					dataMap.put("eD",
							String.valueOf(
									(posChargeData.getExitDate().getTime() - posChargeData.getEntranceDate().getTime())
											/ (1000 * 60)));
					dataMap.put("eD", String
							.valueOf((new Date().getTime() - posChargeData.getEntranceDate().getTime()) / (1000 * 60)));
					// if (Integer.valueOf((String) (dataMap.get("eD"))) > 360) {
					// dataMap.put("eD", "13");
					// }
					logger.info(cardNumber + "防逃费:" + dataMap.toString());
					if (ArrayUtils.contains(Constants.ToQuenePark, parkId.intValue())
							|| park.getDescription().contains("本地计费")) {
						posChargeData.setEntrance(false);
						ActiveMqService.SendTopicWithMac(posChargeData, String.valueOf(parkId), mac,
								park.getPortLeftCount(), monthUserType);
					}
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}

				if (diff < 1000 * 60 * 15) {
					logger.info(cardNumber + "移动支付完成内15分钟离场");
					dataMap.put("my", "0.0");
					long diff1 = (posChargeData.getExitDate().getTime() - posChargeData.getEntranceDate().getTime());
					// dataMap.put("eD", String.valueOf(diff1 / (1000 * 60)));
					dataMap.put("eD", String.valueOf((diff1 / (1000 * 60)) > 15 ? 13 : (diff1 / (1000 * 60))));
					posChargeData.setPaidCompleted(true);
					// posChargeData.setPayType(9);
					posChargeData.setPaidMoney(posChargeData.getChargeMoney());
					posChargeData.setUnPaidMoney(0);
					if (posChargeData.getExitDate() == null) {
						posChargeData.setExitDate1(new Date());
					}
					posChargeData.setOperatorId("15minPaid");
					chargeSerivce.update(posChargeData);
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
				// 超过了15分钟
				else if (posChargeData.getPayType() < 2 && diff < 1000 * 60 * 60 * 48) {
					logger.info(cardNumber + "支付完成15分钟后出场,将重新生成订单");
					// Park
					// park=parkService.getParkById(posChargeData.getParkId());
					// FeeCriterion
					// feeCriterion=feeCriterionService.getById(park.getFeeCriterionId());
					Date incomeDate = new Date(
							payDate.getTime() - (long) (feeCriterion.getFreemins() - 15) * 1000 * 60);
					PosChargeData charge2 = new PosChargeData();
					charge2.setCardNumber(posChargeData.getCardNumber());
					charge2.setParkId(park.getId());
					charge2.setOperatorId("超时15分钟");
					charge2.setParkDesc(posChargeData.getParkDesc());
					charge2.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
					int num = chargeSerivce.insert(charge2);

					jedisClient.set("P-" + parkId + "-" + charge2.getCardNumber(), String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);
					parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);// 补偿加1
					jedisClient.del("P-" + parkId + "-" + cardNumber);
					jedisClient.del("query" + cardNumber);
					logger.info("支付完超15分钟重新入场 " + cardNumber + " id" + charge2.getId());
					jedisClient.set("P-" + parkId + "-" + cardNumber, String.valueOf(charge2.getId()),
							Constants.carMaxPeriod);
					if (num != 1) {
						return Utility.createJsonMsgWithoutMsg(1003, dataMap);
					}
					// 重新查询未缴费
					// queryCharges = chargeSerivce.getDebt(cardNumber);
					queryCharges = chargeSerivce.getDebtWithData(cardNumber, parktoaliparks, realMonthUsers, park,
							isMultiFeeCtriterion, monthUserType, false,monthEndTimeString);
				}
				// 超过两天
				else {
					logger.info(cardNumber + "离上次计费超过两天,没有找到入场记录 判断为异常,免费放行");
					dataMap.put("my", "0.0");
					return Utility.createJsonMsgWithoutMsg(1001, dataMap);
				}
			}

			PosChargeData payRet = new PosChargeData();
			int tmpnn = 0;
			logger.info(cardNumber + "计费完毕后处理");
			int num = 0;
			if (!queryCharges.isEmpty()) {
				for (PosChargeData posChargeData : queryCharges) {
					posChargeData.setPosId(mac);
					if (posChargeData.getParkId() == parkId.intValue()) {
						posChargeData.setPaidCompleted(true);
						posChargeData.setPaidMoney(posChargeData.getChargeMoney());
						posChargeData.setUnPaidMoney(0);
						posChargeData.setPayType(9);
						posChargeData.setPosId(mac);
						// posChargeData.setOperatorId("道闸");
						if (tmpnn == 0) {
							payRet = posChargeData;
							tmpnn++;
						} else {
							posChargeData.setChargeMoney(0);
							posChargeData.setPaidMoney(0);
						}
						chargeSerivce.update(posChargeData);
					} else {
						posChargeData.setChargeMoney(0.0);
						posChargeData.setPaidCompleted(true);
						posChargeData.setPayType(9);
						chargeSerivce.update(posChargeData);
					}
				}

				shouldChargeMoney = payRet.getChargeMoney();
				num = chargeSerivce.update(payRet);
			} else {
				logger.info(cardNumber + "查询计费结果为空!!!");
				return Utility.createJsonMsg(1002, "计费结果为空");
			}

			switch (monthUserType) {
			case 0:
				if (parkcarauthority.getMonth() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 1:
				charge.setParkDesc(park.getName() + "-预约");
				if (parkcarauthority.getYuyue() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 2:
				charge.setParkDesc(park.getName() + "-储值卡");
				if (parkcarauthority.getTypea() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 3:
				charge.setParkDesc(park.getName() + "-包月A2");
				if (parkcarauthority.getTypeb() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 4:
				charge.setParkDesc(park.getName() + "-包月B");
				if (parkcarauthority.getTypec() != true) {
					dataMap.put("aT", "0");
				}
				break;
			case 5:
				charge.setParkDesc(park.getName() + "-包月D");
				if (parkcarauthority.getTyped() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 6:
				charge.setParkDesc(park.getName() + "-包月E");
				if (parkcarauthority.getTypee() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 8:
				charge.setParkDesc(park.getName() + "-包月过期");
				if (parkcarauthority.getMonthexpired() != true) {
					dataMap.put("aT", "0");

				}
				break;
			case 9:
				if (parkcarauthority.getTemporary() != true && shouldChargeMoney > 0) {
					dataMap.put("aT", "0");
				}
				if (parkcarauthority.getTemporary0() != true && shouldChargeMoney < 0.1) {
					dataMap.put("aT", "0");
				}

				break;
			default:
				break;
			}

			if (parknoticeauthority != null && parknoticeauthority.getWeixin() == true && !queryCharges.isEmpty()) {
				Map<String, String> argstoali = new HashMap<>();
				argstoali.put("parkName", payRet.getParkDesc());
				argstoali.put("carNumber", payRet.getCardNumber());
				argstoali.put("enterTime",
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getEntranceDate()));
				argstoali.put("exitTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(payRet.getExitDate()));
				ActiveMqService.SendWithQueueName(JsonUtils.objectToJson(argstoali), "weixinOutInfo");
			}

			if (payRet.getEntranceDate() == null) {

				return Utility.createJsonMsg(1003, "无入场记录");
			}

			long diff2 = (new Date().getTime() - payRet.getEntranceDate().getTime());
			dataMap.put("eD", String.valueOf((diff2 / (60 * 1000)) > 636000 ? 13 : (diff2 / (60 * 1000))));
			if (!isRealMonthUser) {
				dataMap.put("my", String.valueOf(payRet.getChargeMoney()));
			}
			if (isMultiCarsOneCarport && isRealMonthUser && realMonthUsers.size() > 1) {
				if (payRet.getChargeMoney() > 0) {
					dataMap.put("my", String.valueOf(payRet.getChargeMoney()));
					dataMap.put("uT", "0");
				}
			}
			if (payRet.getOther2() != null && payRet.getOther2().equals("工行免密")) {
				dataMap.put("my", "0.0");
				dataMap.put("aT", "1");
				logger.info(cardNumber + "免密开闸");
				payRet.setPayType(7);
			}
			// 发送到队列
			if (ArrayUtils.contains(Constants.parkToQuene, parkId.intValue())
					|| park.getDescription().contains("工行停车场")) {
				ActiveMqService.SendPosChargeData(JsonUtils.objectToJson(payRet));
			}
			num = chargeSerivce.update(payRet);
			if (num == 1) {
				Poschargemac poschargemac = new Poschargemac();
				poschargemac.setMacidout((int) info.get("macId"));
				poschargemac.setPoschargeid(payRet.getId());
				// posChargeMacService.insertSelective(poschargemac);
				jedisClient.set("out" + mac, String.valueOf(payRet.getId()));
				logger.info(cardNumber + "出场成功!" + dataMap.toString());

				parkService.updateLeftPortCount(parkId, park.getPortLeftCount() + 1);
				return Utility.createJsonMsgWithoutMsg(1001, dataMap);
			} else {
				return Utility.createJsonMsg(1001, "ok");
			}
		}
	}

	@RequestMapping(value = "touchedNotify", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String touchedNotify(@RequestBody Map<String, String> args) throws Exception {
		String mac = args.get("mac");
		String cardNumber = args.get("cardNumber");
		boolean largeCar = Boolean.parseBoolean(args.get("largeCar"));
		PosChargeData charge = new PosChargeData();
		charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		Map<String, Object> ret = new HashMap<String, Object>();
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		Map<String, Object> info = infos.get(0);
		if (info == null) {
			return Utility.createJsonMsg(1002, "fail");
		}
		// Map<String, Object> dataMap = new TreeMap<String, Object>();
		int channelFlag = (int) info.get("channelFlag");
		Integer parkId = (Integer) info.get("parkID");
		// String parkName=(String) info.get("Name");
		List<Monthuser> monthusers = monthUserService.getByCarnumberAndPark(cardNumber, parkId);
		Park park = parkService.getParkById(parkId);
		if (channelFlag == 1) {
			Map<String, Object> argMap = new HashMap<>();
			FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());
			argMap.put("parkId", "bd1879d2-bc89-48e5-9e46-927d0f4ec56f");
			argMap.put("plateNum", cardNumber);
			argMap.put("enterTime", new SimpleDateFormat(Constants.DATEFORMAT).format(charge.getEntranceDate()));
			argMap.put("chargeStandard", feeCriterion.getExplaination());
			logger.info("调用贵州入场接口..参数" + argMap.toString());
			try {
				Map<String, Object> infoReturn = HttpUtil
						.post("https://www.gkzhxxsj.com/parking/api/device/barrier/enterCar", argMap);
				logger.info("调用贵州入场接口..返回结果" + infoReturn.toString());
			} catch (Exception e) {
				// TODO: handle exception
			}
		} else {
			Map<String, Object> argMap = new HashMap<>();
			FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());
			try {

				argMap.put("parkId", "bd1879d2-bc89-48e5-9e46-927d0f4ec56f");
				argMap.put("plateNum", cardNumber);
				argMap.put("leaveTime", new SimpleDateFormat(Constants.DATEFORMAT).format(charge.getEntranceDate()));
				// argMap.put("chargeStandard", feeCriterion.getExplaination());
				logger.info("调用贵州出场接口..参数" + argMap.toString());
				Map<String, Object> infoReturn = HttpUtil
						.post("https://www.gkzhxxsj.com/parking/api/device/barrier/leaveCar", argMap);
				logger.info("调用贵州出场接口..结果" + infoReturn.toString());
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		return Utility.createJsonMsg(1001, "ok");
	}

	public String getTimeDiff(Long diff) {
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long nm = 1000 * 60;// 一分钟的毫秒数
		long ns = 1000;// 一秒钟的毫秒数

		long day = diff / nd;// 计算差多少天
		long hour = diff % nd / nh;// 计算差多少小时
		long min = diff % nd % nh / nm;// 计算差多少分钟
		long sec = diff % nd % nh % nm / ns;// 计算差多少秒//输出结果

		if (day != 0) {
			return day + "天" + hour + "小时" + min + "分钟";
		}
		if (day == 0 && hour != 0) {
			return hour + "小时" + min + "分钟";
		}
		if (day == 0 && hour == 0) {
			return min + "分钟";
		}
		return day + "天" + hour + "小时" + min + "分钟";
	}

	@RequestMapping(value = "getTypeByMac", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String getTypeByMac(@RequestBody Map<String, String> args) {
		String mac = args.get("mac");
		List<Map<String, Object>> infos = hardwareService.getInfoByMac(mac);
		if (infos.isEmpty()) {
			return Utility.createJsonMsg(1002, "fail");
		}
		Map<String, Object> info = infos.get(0);
		Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
		int channelFlag = (int) info.get("channelFlag");
		if (channelFlag == 1) {
			dataMap.put("channelType", "in");
		} else {
			dataMap.put("channelType", "out");
		}
		dataMap.put("time", new SimpleDateFormat(Constants.DATEFORMAT).format(new Date()));
		return Utility.createJsonMsg(1001, "success", dataMap);
	}

	/***
	 * 查询是否已缴费接口
	 * 
	 * @throws ParseException
	 *
	 ***/
	@RequestMapping(value = "queryChargeStatus", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String queryChargeStatus(@RequestBody Map<String, String> args) throws ParseException {
		String carNumber = args.get("carNumber");
		Map<String, Object> ret = new HashMap<String, Object>();
		List<PosChargeData> posChargeDataList = chargeSerivce.getLastRecord(carNumber, 1);
		if (posChargeDataList.isEmpty()) {
			ret.put("status", 1002);
			ret.put("message", "没有停车记录!");
			return Utility.gson.toJson(ret);
		}
		PosChargeData posChargeData = posChargeDataList.get(0);
		if (!posChargeData.isPaidCompleted()) {
			ret.put("status", 1002);
			ret.put("message", "未完成支付!");
			ret.put("body", posChargeData);
			return Utility.gson.toJson(ret);
		}
		int payType = posChargeData.getPayType();
		if (payType == 0) {
			List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
			if (alipayrecords.isEmpty()) {
				ret.put("status", 1002);
				ret.put("message", "非支付宝支付!");
				return Utility.gson.toJson(ret);
			}
			Date payDate = alipayrecords.get(0).getDate();
			long diff = new Date().getTime() - payDate.getTime();
			if (diff < 1000 * 60 * 15) {
				ret.put("status", 1001);
				ret.put("message", "已完成支付!");
				return Utility.gson.toJson(ret);
			} else {
				Park park = parkService.getParkById(posChargeData.getParkId());
				FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());
				Date incomeDate = new Date(payDate.getTime() - feeCriterion.getFreemins() * 1000 * 60);
				PosChargeData charge = new PosChargeData();
				charge.setCardNumber(posChargeData.getCardNumber());
				charge.setParkId(park.getId());
				charge.setParkDesc(posChargeData.getParkDesc());
				charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
				int num = chargeSerivce.insert(charge);
				ret.put("status", 1002);
				ret.put("message", "超时出场,还有未完成支付!");
				ret.put("body", charge);
				return Utility.gson.toJson(ret);
			}
		} else {
			List<Alipayrecord> alipayrecords = alipayrecordService.getByPosChargeId(posChargeData.getId());
			if (alipayrecords.isEmpty()) {
				ret.put("status", 1002);
				ret.put("message", "非支付宝支付!");
				return Utility.gson.toJson(ret);
			}
			Date payDate = alipayrecords.get(0).getDate();
			long diff = new Date().getTime() - payDate.getTime();
			if (diff < 1000 * 60 * 15) {
				ret.put("status", 1001);
				ret.put("message", "已完成支付!");
				return Utility.gson.toJson(ret);
			} else {
				Park park = parkService.getParkById(posChargeData.getParkId());
				FeeCriterion feeCriterion = feeCriterionService.getById(park.getFeeCriterionId());
				Date incomeDate = new Date(payDate.getTime() - feeCriterion.getFreemins() * 1000 * 60);
				PosChargeData charge = new PosChargeData();
				charge.setCardNumber(posChargeData.getCardNumber());
				charge.setParkId(park.getId());
				charge.setParkDesc(posChargeData.getParkDesc());
				charge.setEntranceDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(incomeDate));
				int num = chargeSerivce.insert(charge);
				ret.put("status", 1002);
				ret.put("message", "超时出场,还有未完成支付!");
				ret.put("body", charge);
				return Utility.gson.toJson(ret);
			}
		}
	}

	private String getParkingTimeLen(long exitTime, long enterTime) {
		long exitDate = (exitTime - enterTime) / (1000 * 60);
		if (exitDate > 0) {
			return String.valueOf(exitDate);
		} else {
			return "13";
		}
	}

	private void storageCardLogic(List<Monthuser> monthusers, PosChargeData posChargeData) {
		monthusers.get(0).setPayment((float) (monthusers.get(0).getPayment() - posChargeData.getPaidMoney()));
		monthUserService.updateByPrimaryKey(monthusers.get(0));
		Monthuserconsumerrecord monthuserconsumerrecord = new Monthuserconsumerrecord();
		monthuserconsumerrecord.setBeforerecharge((double) monthusers.get(0).getPayment());
		monthuserconsumerrecord.setParkid(posChargeData.getParkId());
		monthuserconsumerrecord.setParkdesc(posChargeData.getParkDesc());
		monthuserconsumerrecord.setOperationtype(0);
		monthuserconsumerrecord.setPoschargedataid(posChargeData.getId());
		monthuserconsumerrecord.setPlatenumber(posChargeData.getCardNumber());
		monthuserconsumerrecord.setName(monthusers.get(0).getOwner());
		monthuserconsumerrecord.setShouldcharge(posChargeData.getChargeMoney());

		monthusers.get(0).setPayment((float) (monthusers.get(0).getPayment() - posChargeData.getChargeMoney()));
		monthuserconsumerrecord.setBalance((double) monthusers.get(0).getPayment());
		monthuserConsumerService.insertSelective(monthuserconsumerrecord);

	}
}
